tags:

views:

46

answers:

2

Hello, 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 like 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!

A: 

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>"
Mark Westling
Thanks for answering! I don't want to write a = Myobj.new. I want do directly puts Myobj.new(x,y,z) and to get printed x, y, z Thank you
Andrei Ion
+1  A: 

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
agregoire
great! thank you!
Andrei Ion