Skip to content

Instantly share code, notes, and snippets.

@svs
Created April 24, 2012 09:23
Show Gist options
  • Save svs/2478214 to your computer and use it in GitHub Desktop.
Save svs/2478214 to your computer and use it in GitHub Desktop.
Question about Rich Hickey's 'use hashes not classes' statement

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?

@ghoseb
Copy link

ghoseb commented Apr 24, 2012

Not monkey-patching the class, rather extending actual object instances by dynamically mixing in modules.

In that case, you just need an immutable data-structure and some metadata. No need to mess around with opaque, mutable objects.

@svs
Copy link
Author

svs commented Apr 24, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment