Variable -- set value

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

var1 = “@var2

Whot can I set value for variable which name cosist ather variable?
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

“orbit” orbit@klientsky.ru schrieb im Newsbeitrag
news:734420e9.0405310551.2526ea0@posting.google.com

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

var1 = “@var2

var1 = eval @var2

robert

orbit@klientsky.ru (orbit) wrote in message news:734420e9.0405310551.2526ea0@posting.google.com

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

···


Sergey

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

···

On Tue, Jun 01, 2004 at 08:53:47PM +0900, orbit wrote:

orbit@klientsky.ru (orbit) wrote in message news:<734420e9.0405310551.2526ea0@posting.google.com>...
> 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

“orbit” orbit@klientsky.ru schrieb im Newsbeitrag
news:734420e9.0406010348.c89bb58@posting.google.com

orbit@klientsky.ru (orbit) wrote in message
news:734420e9.0405310551.2526ea0@posting.google.com

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

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™.

Regards

robert

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 :slight_smile:

cheers,
Mark

···

On Jun 1, 2004, at 4:53 AM, orbit wrote:

orbit@klientsky.ru (orbit) wrote in message
news:734420e9.0405310551.2526ea0@posting.google.com

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

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

···

On Tue, 1 Jun 2004 20:53:47 +0900, orbit orbit@klientsky.ru wrote:

orbit@klientsky.ru (orbit) wrote in message news:734420e9.0405310551.2526ea0@posting.google.com

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

Michael Campbell wrote:

orbit@klientsky.ru (orbit) wrote in message news:734420e9.0405310551.2526ea0@posting.google.com

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

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”;

···

On Tue, 1 Jun 2004 20:53:47 +0900, orbit orbit@klientsky.ru wrote:


Mark Sparshatt

IMHO, why not a generic replace for every class:

'Object#self=' class??

Thanks,
  Angel

···

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 :slight_smile:

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

···

On Wed, 2 Jun 2004 00:59:58 +0900, Mark Sparshatt msparshatt@yahoo.co.uk wrote:

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”;