Just in time convenience methods


/ Published in: Ruby
Save to your folder(s)

Using instance_eval to abstract values into meaningful methods


Copy this code and paste it in your HTML
  1. time_components = /(\d+):(\d+):(\d+)/.match("17:00:34")
  2. time_components.instance_eval do
  3. def hours; self[1] end
  4. def minutes; self[2] end
  5. def seconds; self[3] end
  6. end
  7.  
  8. time_components.hours # => "17"
  9. time_components.minutes # => "00"
  10. time_components.seconds # => "34"
  11. time_components.class # => MatchData

URL: http://toolmantim.com/article/2006/11/29/instance_eval_brings_sexy_back

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.