Created
July 7, 2014 17:05
-
-
Save asaaki/0c79b47805d9c6ec8dc3 to your computer and use it in GitHub Desktop.
ProcString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "forwardable" | |
class ProcString | |
extend Forwardable | |
def_delegators :stringified_proc, :to_s, *("".methods - BasicObject.methods) | |
attr_reader :proc | |
def initialize(&string_proc) | |
@proc = string_proc | |
end | |
def inspect | |
"#<#{self.class.name} @proc=#{@proc.inspect} @current_data=#{stringified_proc}>" | |
end | |
private | |
def stringified_proc | |
@proc.call.to_s | |
end | |
end | |
# Example | |
pstr = ProcString.new { rand } | |
pstr | |
=> #<ProcString @proc=#<Proc:0x007f9be580acb8> @current_data=0.7499222574795362> | |
puts pstr, pstr, pstr | |
#=> 0.0020494615701293073 | |
#=> 0.8607430906723921 | |
#=> 0.965687955748027 | |
pstr.gsub(/0/,"NULL") | |
#=> "NULL.2NULL5839749741NULL8NULL55" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment