variable -- set value

T

ts

o> Whot can I set value for variable which name cosist ather variable?
o> var1 = "@var2"


Well, if you have the name of an instance variable you can use
#instance_variable_{set,get} (with 1.8) to retrieve, or set, its value

svg% cat b.rb
#!/usr/bin/ruby
class A
def initialize(val)
@a = val
end
end

a = A.new(12)
p a.instance_variable_get("@a")
a.instance_variable_set("@a", 24)
p a.instance_variable_get("@a")
svg%

svg% b.rb
12
24
svg%



Guy Decoux
 
O

orbit

Whot can I set value for variable which name cosist ather variable?

var1 = "@var2"

I want, that as in Perl was
cat p1.pl

#!/usr/bin/perl

$foo = "xo-xo";
print "1 - foo = $foo\n";
$scalarref = \$foo;
$$scalarref = "xe-xe";
print "2 - foo = $foo\n";

../p1.pl
1 - foo = xo-xo
2 - foo = xe-xe
 
M

Michael Neumann

I want, that as in Perl was
cat p1.pl

#!/usr/bin/perl

$foo = "xo-xo";
print "1 - foo = $foo\n";
$scalarref = \$foo;
$$scalarref = "xe-xe";
print "2 - foo = $foo\n";

../p1.pl
1 - foo = xo-xo
2 - foo = xe-xe

Hm, I don't really understand this code without straining my mind.
You probably want this:

foo = ["xo-xo"]
puts "1 - foo = #{ foo[0] }"
scalarref = foo
scalarref[0] = "xe-xe"
puts "1 - foo = #{ foo[0] }"

Or more rubyish:

class ValueHolder
attr_accessor :value
def initialize(value)
@value = value
end
end

foo = ValueHolder.new("xo-xo")
puts "1 - foo = #{ foo.value }"
ref = foo
ref.value = "xe-xe"
puts "1 - foo = #{ foo.value }"

But not sure if that's what you want.
But please don't use the same idiom as in perl, try to do it the
Ruby-way.

Regards,

Michael
 
R

Robert Klemme

orbit said:
(e-mail address removed) (orbit) wrote in message

I want, that as in Perl was
cat p1.pl

#!/usr/bin/perl

$foo = "xo-xo";
print "1 - foo = $foo\n";
$scalarref = \$foo;
$$scalarref = "xe-xe";
print "2 - foo = $foo\n";

./p1.pl
1 - foo = xo-xo
2 - foo = xe-xe

That's not possible in Ruby in a similar way. You could do however:

#!/usr/bin/ruby

class Ref
def initialize(var, env)
var = var.to_s if Symbol === var
@var, @env = var, env
end

def deref
eval( @var, @env )
end

def deref=(x)
Thread.current[:x] = x
begin
eval( "#{@var}=Thread.current[:x]", @env )
ensure
Thread.current[:x] = nil
end
end
end

foo = "xo-xo"
puts "1 - foo = #{foo}"
scalarref = Ref.new( :foo, binding )
scalarref.deref = "xe-xe"
puts "2 - foo = #{foo}"

But what do you need that for? I suspect you won't miss this perlism any
more if you adjust more to the Ruby Way(TM).

Regards

robert
 
M

Mark Hubbart

(e-mail address removed) (orbit) wrote in message


I want, that as in Perl was
cat p1.pl

#!/usr/bin/perl

$foo = "xo-xo";
print "1 - foo = $foo\n";
$scalarref = \$foo;
$$scalarref = "xe-xe";
print "2 - foo = $foo\n";

../p1.pl
1 - foo = xo-xo
2 - foo = xe-xe

You probably want something like this:

b=a="test"
==>"test"
a.replace "hello"
==>"hello"
[a,b]
==>["hello", "hello"]

String#replace replaces the contents of the string with the argument
passed.

But everything in ruby is done by reference, so there is no clean way
of doing what you are talking about that works for every type. This is
one of those things that really bugged me at first, but I eventually
liked; Not being able to do tricks like that doesn't seem to hamper my
code any, and leaves it much easier to understand.

I would recommend leaving the pointers to C and Perl :)

cheers,
Mark
 
M

Michael Campbell

I want, that as in Perl was
cat p1.pl

#!/usr/bin/perl

$foo = "xo-xo";
print "1 - foo = $foo\n";
$scalarref = \$foo;
$$scalarref = "xe-xe";
print "2 - foo = $foo\n";

../p1.pl
1 - foo = xo-xo
2 - foo = xe-xe


I think that even the royal perl monarchy discourage the use of
softrefs as "bad practice", no?
 
M

Mark Sparshatt

Michael said:
I think that even the royal perl monarchy discourage the use of
softrefs as "bad practice", no?
They do, but that example is a hard reference which isn't discouraged.

The following is a soft, or symbolic reference,

$foo = "xo-xo";
print "1 - foo = $foo\n";
$scalarref = "foo"; #"foo" rather than \$foo
$$scalarref = "xe-xe";
print "2 - foo = $foo\n";
 
M

Michael Campbell

They do, but that example is a hard reference which isn't discouraged.

The following is a soft, or symbolic reference,

$foo = "xo-xo";
print "1 - foo = $foo\n";
$scalarref = "foo"; #"foo" rather than \$foo
$$scalarref = "xe-xe";
print "2 - foo = $foo\n";


Ah, right you are. Been a while since I did much with perl.
 
A

Angel Martin

El mar, 01-06-2004 a las 17:40, Mark Hubbart escribió:
String#replace replaces the contents of the string with the argument
passed.

But everything in ruby is done by reference, so there is no clean way
of doing what you are talking about that works for every type. This is
one of those things that really bugged me at first, but I eventually
liked; Not being able to do tricks like that doesn't seem to hamper my
code any, and leaves it much easier to understand.

I would recommend leaving the pointers to C and Perl :)

IMHO, why not a generic replace for every class:

'Object#self=' class??

Thanks,
Angel
 

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
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top