ruby - How to get private variables of instance object without calling method -


is possible call specific method (or getting instance variables) without instance method instance object in ruby?

class foo   def initialize(arg)     @bar = arg   end end  f = foo.new('test') p f #=> "test" (in case, @bar variable without instance method) 

for instance, if example class defined,

ex = example.new ex #=> #<example:0x00000000000000> 

i want do, this.

ex = example.new('hello') ex #=> "hello" 

you can use inspect p , to_s puts

class foo   def initialize(arg)     @bar = arg   end   def inspect     @bar   end   def to_s     @bar   end end  f = foo.new('test') puts f #=> "test" p f #=> "test" 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -