Short cut to do a inspect method for object

I have a object with following instance variables
def initialize(symb)
#puts symb
@symbol=symb

@[email protected][email protected][email protected][email protected][email protected][email protected][email protected]=""
end

Is there a quick way to override the inpect method to return the
instance variable name and values in a array?

On Monday 24 July 2006 07:00, removed_email_addre[email protected] wrote:

I have a object with following instance variables
def initialize(symb)
#puts symb
@symbol=symb

@[email protected][email protected][email protected][email protected][email protected][email protected][email protected]=""
end

Is there a quick way to override the inpect method to return the
instance variable name and values in a array?

class A
def initialize
@a=:aa
@b=:bb
@c=:cc
end
end
A.new.inspect

“#<A:0xb7c197a4 @c=:cc, @a=:aa, @b=:bb>”

class A
def inspect
instance_variables.map{ |iv| [ iv, eval(iv).inspect ] }
# yes, i’m ev(a|i)l here… use instance_variable_get instead - it’s
just
# too long for this line :expressionless:
end
end

A.new.inspect

[["@c", :cc], ["@a", :aa], ["@b", :bb]]

class A
def inspect
instance_variables.map do |iv|
“#{iv}=#{instance_variable_get(iv).inspect}”
end .join(’, ')
end
end

A.new.inspect

“@c=:cc, @a=:aa, @b=:bb”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs