I have a newbie question! I want to do something like this:
puts Example.new([a,b,c])
and the result to be
=> a,b,c
I tried something li开发者_Go百科ke this:
class Example
attr_accessor :something
def initialize(something)
@something = something
puts @something
end
end
It works but not how I want it! Thank you!
Are you looking to print (in readable form) an object? Try using the inspect method.
class Myobj
attr_accessor :x, :y, :z
end
a = Myobj.new
a.x = 1; a.y = 2; a.z = 3
a.inspect #=> "#<Myobj:0x1bc48950 @y=2, @x=1, @z=3>"
Would something like this work ?
class Example
def initialize(args = [])
@args = args
end
def to_s
@args.join(",")
end
end
puts Example.new([1,2,3])
>> 1,2,3
精彩评论