Skip to content

Instantly share code, notes, and snippets.

@asaaki
Created July 7, 2014 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asaaki/0c79b47805d9c6ec8dc3 to your computer and use it in GitHub Desktop.
Save asaaki/0c79b47805d9c6ec8dc3 to your computer and use it in GitHub Desktop.
ProcString
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