Hash vs methods

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.

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

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

Thanks for responses. So it’s possible to call include? on a method?

dom_class.include?(“sortable”)

Phil C. wrote in post #992790:

On Thu, Apr 14, 2011 at 10:09 AM, John M. [email protected]
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

On Thu, Apr 14, 2011 at 10:09 AM, John M. [email protected]
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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs