So I don't have a model called Person, but I use hashes like {:name => "Siddharth", :sex => "Y"}. Great. Now I want to validate the presence of certain fields before persisting to the database. What do I do?
Module PersonValidation
function validate_presence_of_name(person_hash)
person_hash[:errors][:name] = "EEENKKK!" unless person_hash[:name]
end
...
...
end
So I must be missing something because how is this different from a class in a dynamic language? The above approach means you can easily swap out functions in one module for another and thus not tie the validation implementation to the structure of the loan hash, but this is trivially simple in Ruby even using classes. Is there some other benefit?
I don't seen any advantage. You might as well have passed a
person_obj
instead ofperson_hash
and saidunless person_obj.name
. In Ruby, a class gives you two things: a container to attach the defined methods to (klass), and a scope to look up method and variable names. Hash is a container; using a Hash instead of a class is not the same.