short cut to do a inspect method for object

J

junkone

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

@name=@exchange=@news=@summary=@sector=@industry=@category=@markedBad=""
end

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

Michael Fellinger

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

@name=3D@exchange=3D@news=3D@summary=3D@sector=3D@industry=3D@category=3D= @markedBad=3D""
end

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

class A
=A0 def initialize
=A0 =A0 @a=3D:aa
=A0 =A0 @b=3D:bb
=A0 =A0 @c=3D:cc
=A0 end
end
A.new.inspect
# "#<A:0xb7c197a4 @c=3D:cc, @a=3D:aa, @b=3D:bb>"


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

A.new.inspect
# [["@c", :cc], ["@a", :aa], ["@b", :bb]]


class A
=A0 def inspect
=A0 =A0 instance_variables.map do |iv|
=A0 =A0 =A0 "#{iv}=3D#{instance_variable_get(iv).inspect}"
=A0 =A0 end .join(', ')
=A0 end
end

A.new.inspect
# "@c=3D:cc, @a=3D:aa, @b=3D:bb"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top