Hey all, Hash vs methods may seem like a silly question, but in certain situations I don''t understand why you would use a method over a hash. For example, take a look at this method: #this is defined in a class called core.rb def assign_attributes(attributes) attributes.each_pair { |k,v| send("#{k}=", v) if respond_to?("#{k}=") } if attributes end This method gets called with this: assign_attributes @options Options is a hash when it gets passed into argument list which contains key/value pairs: @options[:dom_id] = @options.delete(:id) @options[:dom_class] = @options.delete(:class) assign_attributes @options It appears that assign_attributes method is taking the key/value pairs and converting them into setter and getter methods in a class called table builder: attr_accessor :dom_class, :dom_id So basically we pass a hash and the each_pair method iterates through the key/value pairs and uses the send method to call a setter #{k} back on the table builder class and passing ''v'' as a parameter so now the getter method :dom_class or dom_id can be used to retrieve a value like here: def sortable? dom_class && dom_class.include?("sortable") end The dom_class is called and if it exists then we check if it includes the string ''sortable'', but now it appears the method is being used as an array or can include? be called on a mehtod like a method chain? Or is dom_class not even a method? And if it''s not then why even bother creating setter/getter methods when you could have just used a hash? Or am I misunderstanding what assign_attributes is doing? Thanks for response. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> Hey all, > > Hash vs methods may seem like a silly question, but in certain > situations I don''t understand why you would use a method over a hash. > For example, take a look at this method: > > #this is defined in a class called core.rb > def assign_attributes(attributes) > attributes.each_pair { |k,v| send("#{k}=", v) if respond_to?("#{k}=") > } if attributes > endUsing a method would let one override what happens when you assign to that attribute.. say you need to do something magical with it... validate it... convert it... set something else somewhere else... that sort of thing... -philip> This method gets called with this: > > assign_attributes @options > > Options is a hash when it gets passed into argument list which contains > key/value pairs: > > @options[:dom_id] = @options.delete(:id) > @options[:dom_class] = @options.delete(:class) > assign_attributes @options > > It appears that assign_attributes method is taking the key/value pairs > and converting them into setter and getter methods in a class called > table builder: > > attr_accessor :dom_class, :dom_id > > So basically we pass a hash and the each_pair method iterates through > the key/value pairs and uses the send method to call a setter #{k} back > on the table builder class and passing ''v'' as a parameter so now the > getter method :dom_class or dom_id can be used to retrieve a value like > here: > > def sortable? > dom_class && dom_class.include?("sortable") > end > > The dom_class is called and if it exists then we check if it includes > the string ''sortable'', but now it appears the method is being used as an > array or can include? be called on a mehtod like a method chain? Or is > dom_class not even a method? And if it''s not then why even bother > creating setter/getter methods when you could have just used a hash? Or > am I misunderstanding what assign_attributes is doing? > > Thanks for response. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Philip Hallstrom wrote in post #992605:>> end > Using a method would let one override what happens when you assign to > that attribute.. say you need to do something magical with it... > validate it... convert it... set something else somewhere else... that > sort of thing...And, just as importantly, the method provides encapsulation, effectively hiding the internal implementation from the public API. This provides the flexibility of modifying or improving the internal implementation without affecting the consumers of the class. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks for responses. So it''s possible to call include? on a method? dom_class.include?("sortable") -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Thu, Apr 14, 2011 at 10:09 AM, John Merlino <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Thanks for responses. So it''s possible to call include? on a method? > > dom_class.include?("sortable") >Well, you aren''t calling include? on the method, per se... you''re calling include? on _whatever thing the method is returning_. So, if the returned object responds to include?, then sure, you can.> > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Phil Crissman wrote in post #992790:> On Thu, Apr 14, 2011 at 10:09 AM, John Merlino <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > wrote: > >> Thanks for responses. So it''s possible to call include? on a method? >> >> dom_class.include?("sortable") >> > > Well, you aren''t calling include? on the method, per se... you''re > calling > include? on _whatever thing the method is returning_. So, if the > returned > object responds to include?, then sure, you can.understood, thanks -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.