Skip to content

Instantly share code, notes, and snippets.

@hitode909
Created February 18, 2012 06:48
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 hitode909/1857903 to your computer and use it in GitHub Desktop.
Save hitode909/1857903 to your computer and use it in GitHub Desktop.
module PrintSelf
def inspect
args = self.method(:initialize).parameters.map{ |param|
self.instance_variable_get("@#{param[1]}".to_sym).inspect
}
"#{self.class}.new(#{args.join(", ")})"
end
end
$:.push('.')
require 'print_self'
class Human
include PrintSelf
def initialize(name, age)
@name = name
@age = age
end
end
p Human.new('inoue', 13) # => Human.new("inoue", 13)
p eval(Human.new('inoue', 13).inspect) # => Human.new("inoue", 13)
p eval(eval(eval(eval(Human.new('inoue', 13).inspect).inspect).inspect).inspect) # => Human.new("inoue", 13)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment