I defined a function: to_s in pry, but I can't name it. Where does this method go and how can I call it?
pry(main)> def to_s pry(main)* 'hello' pry(main)* end pry(main)> to_s => "main"
My ruby โโversion is 2.1.2
After reading some answers and searching, I think I have the correct answer:
when a method is defined in irb or pry, it goes to Object.instance_methods
[1] pry(main)> def to_s [1] pry(main)* 'hello' [1] pry(main)* end => :to_s [2] pry(main)> def hello [2] pry(main)* 'world' [2] pry(main)* end => :hello [4] pry(main)> Object.instance_methods(false) => [:pry, :__binding__, :to_s, :hello]
- How can I call him?
These new methods can be called in a new object.
[6] pry(main)> Object.new.to_s => "hello"
The reason I cannot call to_s at the top level is because main is the special object that the #to_s and #inspect .
[5] pry(main)> singleton_class.instance_methods(false) => [:to_s, :inspect]
source share