Symbol usage help

rubyists-

given

a = 42
b = ‘forty-two’

is it possible to do something like :

%w( a b ).each do |s|
puts “#{s} = #{s.intern.value}”
end

to output

a = 42
b = forty-two

obviously, the Symbol#value method does not exist, but this should be clear
enough…

thanks.

-a

···

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

ahoward wrote:

rubyists-

given

a = 42
b = ‘forty-two’

is it possible to do something like :

%w( a b ).each do |s|
puts “#{s} = #{s.intern.value}”
end

to output

a = 42
b = forty-two

obviously, the Symbol#value method does not exist, but this should be clear
enough…

Instead of

s.intern.value

Try

eval s

or

eval s.intern.to_s

ahoward wrote:

rubyists-

given

a = 42
b = ‘forty-two’

is it possible to do something like :

%w( a b ).each do |s|
puts “#{s} = #{s.intern.value}”
end

to output

a = 42
b = forty-two

obviously, the Symbol#value method does not exist, but this should be clear
enough…

What about this?

 a = 42
 b = 'forty-two'

 %w( a b ).each do |s|
   puts "#{s} = #{eval(s.intern.to_s)}"
 end

Lyle

In article Pine.LNX.4.33.0211191902010.29118-100000@eli.fsl.noaa.gov,

rubyists-

given

a = 42
b = ‘forty-two’

is it possible to do something like :

%w( a b ).each do |s|
puts “#{s} = #{s.intern.value}”
end

to output

a = 42
b = forty-two

obviously, the Symbol#value method does not exist, but this should be clear
enough…

How about?:

irb(main):013:0> %w( a b ).each do |s|
irb(main):014:1* puts “#{s} = #{eval s}”
irb(main):015:1> end
a = 42
b = forty-two

But actually, you’re list doesn’t contain symbols but strings which stand
for the names of your two variables a, and b. So ‘s’ above isn’t a Symbol
but a String.

probably what you really want is:

[:a,:b].each do |s|
puts #{s} = #{eval s.id2name}
end

Phil

···

ahoward ahoward@fsl.noaa.gov wrote:

hmmm. this does work, but the string eval’d is actually dynamically generated
from a cgi program - so i’d like to do it safely. i could do

if Class === (klass = (eval s.intern)).type
object = klass.new
end

and at least know it was a class - but i’ve already evaluated the string at
this point…

-a

···

On Wed, 20 Nov 2002, Joel VanderWerf wrote:

Instead of

s.intern.value

Try

eval s

or

eval s.intern.to_s

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Hi –

Instead of

s.intern.value

Try

eval s

or

eval s.intern.to_s

hmmm. this does work, but the string eval’d is actually dynamically generated
from a cgi program - so i’d like to do it safely. i could do

if Class === (klass = (eval s.intern)).type
object = klass.new
end

and at least know it was a class - but i’ve already evaluated the string at
this point…

Actually this:

Class === obj.type

is always true, because it’s testing to see whether obj.type is a
Class.

Maybe you could do:

if Class === const_get(s)
obj = s.new
end

David

···

On Wed, 20 Nov 2002, ahoward wrote:

On Wed, 20 Nov 2002, Joel VanderWerf wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hello ahoward,

Tuesday, November 19, 2002, 11:50:48 PM, you wrote:

hmmm. this does work, but the string eval’d is actually
dynamically generated from a cgi program - so i’d like to do it
safely. i could do

it is safer to use hash in such situations:

hash[‘a’] = 12
hash[‘b’] = ‘dsd’

···


Best regards,
Bulat mailto:bulatz@integ.ru

it seems to be a valid test for me?

irb(main):002:0> Class === (eval s).type
NameError: (eval):1:in irb_binding': undefined local variable or method foobar’ for #Object:0x40190ce0 from (irb):2
from (irb):2

irb(main):003:0> s = ‘String’
“String”
irb(main):004:0> Class === (eval s).type
true

note the original had a typo (s.intern). don’t know what i was thinking, but
that doesn’t really affect what you were saying does it?

-a

···

On Wed, 20 Nov 2002 dblack@candle.superlink.net wrote:

Actually this:

Class === obj.type

is always true, because it’s testing to see whether obj.type is a
Class.

Maybe you could do:

if Class === const_get(s)
obj = s.new
end

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Hi –

Actually this:

Class === obj.type

is always true, because it’s testing to see whether obj.type is a
Class.

Maybe you could do:

if Class === const_get(s)
obj = s.new
end

it seems to be a valid test for me?

irb(main):002:0> Class === (eval s).type
NameError: (eval):1:in irb_binding': undefined local variable or method foobar’ for #Object:0x40190ce0 from (irb):2
from (irb):2

Well, admittedly Class === obj.type is only true if obj is a Ruby
object :slight_smile: In your example, the eval raises a NameError, so the
=== test is never even performed.

irb(main):003:0> s = ‘String’
“String”
irb(main):004:0> Class === (eval s).type
true

The #type method always returns a Class object, and this:

Class === SomeClass

is always true. So

Class === some_object.type

will always be true.

In the example we’re discussing, the syntax sort of chases its own
tail, in the sense that we’ve got a String “String”, eval’ing which
yields the Class object String – which, of course, is the class of
the thing we just eval’d… See if this little battery of tests
helps clarify it:

irb(main):001:0> s = ‘String’
“String”
irb(main):002:0> s.type
String
irb(main):003:0> eval(s)
String
irb(main):004:0> eval(s).type
Class
irb(main):005:0> Class === s
false
irb(main):006:0> Class === s.type
true
irb(main):007:0> Class === eval(s)
true
irb(main):008:0> Class === eval(s).type
true
irb(main):009:0> s.type == eval(s)
true

Note the one false result: s itself is a String, not a Class. But the
class of s is a Class, and the class of String (namely, Class) is also
a Class.

David

···

On Wed, 20 Nov 2002, ahoward wrote:

On Wed, 20 Nov 2002 dblack@candle.superlink.net wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav