/ Published in: Ruby
Using instance_eval to abstract values into meaningful methods
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
time_components = /(\d+):(\d+):(\d+)/.match("17:00:34") time_components.instance_eval do def hours; self[1] end def minutes; self[2] end def seconds; self[3] end end time_components.hours # => "17" time_components.minutes # => "00" time_components.seconds # => "34" time_components.class # => MatchData
URL: http://toolmantim.com/article/2006/11/29/instance_eval_brings_sexy_back