How about doing for params within ApplicationController what ActiveRecord has with find_by_column_name : module ActionController class Base def method_missing(method_id, *args, &block) if method_id.to_s =~ /^param_(.*)/ params[$1.intern] else super end end end end Then, instead of params[:first_name] params[:amount] write param_first_name param_amount and so on. Seems a lot easier to write and read. Stephan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Neat trick, but I think it''s harder to read. My editor easily identifies hashes / symbols, etc. I''d prefer something like params.id params.name or params.user.id params.user.first_name instead of params[:user][:first_name] :) On Nov 29, 2007 10:54 PM, Stephan Wehner <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > How about doing for params within ApplicationController what > ActiveRecord has with find_by_column_name : > > module ActionController > class Base > def method_missing(method_id, *args, &block) > if method_id.to_s =~ /^param_(.*)/ > params[$1.intern] > else > super > end > end > end > end > > > Then, instead of > > params[:first_name] > params[:amount] > > write > > param_first_name > param_amount > > and so on. Seems a lot easier to write and read. > > Stephan > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Brian Hogan wrote:> I''d prefer something like > > params.id > params.name > > > or > > params.user.id > params.user.first_nameYes that is more uniform -- I forgot about the nested hashes. Stephan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
What you''re asking for is really quite simple and shouldn''t slow you down. However, it''ll get confused when you have a word set as a key as both a string and a symbol, but you should avoid that anyways. http://pastie.caboo.se/123603 Good luck! On Nov 30, 12:11 am, Stephan Wehner <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Brian Hogan wrote: > > I''d prefer something like > > > params.id > > params.name > > > or > > > params.user.id > > params.user.first_name > > Yes that is more uniform -- I forgot about the nested hashes. > > Stephan > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Brian Hogan wrote:> Neat trick, but I think it''s harder to read. My editor easily identifies > hashes / symbols, etc. > I''d prefer something like > > params.id > params.name > > > or > > params.user.id > params.user.first_name > > instead of > > params[:user][:first_name]Actually, another twist is that names which are params object instance methods (such as to_s) are not going to work with this. params.to_s -> ?? params.display -> ?? params.dup -> ?? Unpleasant surprises seem unavoidable. So maybe the whole thing is not such a good idea after all. Underscore doesn''t work, period doesn''t work. Stephan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Well, no... that''s why you''re using method_missing. to_s is defined, so method_missing doesn''t get fired. But I do think it''s really more of a waste of time :) On Nov 30, 2007 1:14 AM, Stephan Wehner <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Brian Hogan wrote: > > Neat trick, but I think it''s harder to read. My editor easily identifies > > hashes / symbols, etc. > > I''d prefer something like > > > > params.id > > params.name > > > > > > or > > > > params.user.id > > params.user.first_name > > > > instead of > > > > params[:user][:first_name] > > > Actually, another twist is that names which are params object instance > methods (such as to_s) are not going to work with this. > > params.to_s -> ?? > params.display -> ?? > params.dup -> ?? > > Unpleasant surprises seem unavoidable. > > So maybe the whole thing is not such a good idea after all. Underscore > doesn''t work, period doesn''t work. > > > Stephan > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Brian Hogan wrote:> Well, no... that''s why you''re using method_missing. to_s is defined, so > method_missing doesn''t get fired.What I was thinking of is when method_missing is not invoked for "to_s" (and the other examples), then the value produced would be different from params[:to_s] (in most cases). In other words, the "interface" is not uniform. Programmers would need to avoid certain names when using params.name, unlike when one just uses the existing params[:name]. Stephan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You guys are very right, and this is more for just-so-you-know than actuall use in a production environment, but you could use it if your hash doesnt have a lot of keys. How many of you have had a hash with a key of to_a? Alternatively, you could do something else, which I''ll have up in a few. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Boutros wrote:> You guys are very right, and this is more for just-so-you-know than > actuall use in a production environment, but you could use it if your > hash doesnt have a lot of keys. How many of you have had a hash with a > key of to_a?It''s not only to_a, to_s: Hash objects have a lot of methods, for example: clear, collect, default, display, entries, index, shift, select, store, size, sort, type, update, values Even Object method names make good form parameter names, such as inspect, send, id, freeze, extend, display, method, type For some application these names would be convenient to use in this other way.> Alternatively, you could do something else, which I''ll > have up in a few.Sounds good! In the mean time, I''m thinking double-underscores might do the trick as in param__user__id for params[:user][:id] (3 characters shorter). Stephan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
It''s a fun excercise but I don''t think what''s there is really that hard to read or use. I''m just interested in this for the academic side of things. It''s neat to see what Ruby can do. On Nov 30, 2007 12:25 PM, Stephan Wehner <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Michael Boutros wrote: > > You guys are very right, and this is more for just-so-you-know than > > actuall use in a production environment, but you could use it if your > > hash doesnt have a lot of keys. How many of you have had a hash with a > > key of to_a? > > It''s not only to_a, to_s: Hash objects have a lot of methods, for > example: > > clear, collect, default, display, entries, index, shift, select, > store, size, sort, type, update, values > > Even Object method names make good form parameter names, such as > > inspect, send, id, freeze, extend, display, method, type > > For some application these names would be convenient to use in this > other way. > > > Alternatively, you could do something else, which I''ll > > have up in a few. > > Sounds good! In the mean time, I''m thinking double-underscores might do > the trick as in param__user__id for params[:user][:id] (3 characters > shorter). > > Stephan > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
It''s really simple to add extra "characters" to make sure there''s no overlapping. For example, "hash.key.user.id" could be done quite easily, adapting from my code earlier. I''ll have that up in a moment. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
It''s really simple to add extra "characters" to make sure there''s no overlapping. For example, "hash.key.user.id" could be done quite easily, adapting from my code earlier. I''ll have that up in a moment. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Here: http://pastie.caboo.se/123991 The method "key" just returns the hash, then the code from before kicks in. You can change key to whatever you want to make it better for you. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---