> 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

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...

-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@googlegroups.com.
> To unsubscribe from this group, send email to 
> rubyonrails-talk+unsubscr...@googlegroups.com.
> 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@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to