Posted By

vanne on 01/23/07


Tagged

ruby


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


Just in time convenience methods


 / Published in: Ruby
 

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

Using instance_eval to abstract values into meaningful methods

  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

Report this snippet  

You need to login to post a comment.