Storing and using references to variables

Hi,
I want to store a reference to a scalar variable (in perl parlance)
in another variable and then assign a value to the stored variable. I
have done this for contiainers by simply assigning the container to the
variable:

ref = a = []

ref[0] = ‘x’

leaves a[0] == ‘x’

as it should.

I want to be able to do something similar but using a simple variable
instead of an array. E.g. in perl

$ref = $a

$$ref = ‘x’

but I can not find anything about explicitly dereferencing a variable.

what I have done in the mean time is to use an array and only use the
first element.

Cheers, Russell

On 7/8/06, Russell F. [email protected] wrote:

Hi,
I want to store a reference to a scalar variable (in perl parlance)
in another variable and then assign a value to the stored variable.

In Ruby all variables are references to an object, there is no way to
make a reference to a reference. If you want to make a change to a
string that’s referred to by two objects use the in-place operators,
they normally are suffixed with an !.

eg:

foo = ‘bar’
baz = foo
baz = baz.gsub(‘bar’,‘foo’)
puts [baz, foo].join(‘-’)
=> foo-bar

vs

foo = ‘bar’
baz = foo
baz.gsub!(‘bar’,‘foo’)
puts [baz, foo].join(‘-’)
=> foo-foo

If this isn’t appropriate you’ll have to use a container as you are at
the moment.

Russell F. wrote:

leaves a[0] == ‘x’
but I can not find anything about explicitly dereferencing a variable.

what I have done in the mean time is to use an array and only use the
first element.

In Ruby, all variables (except for number objects) are references and
there is no need to implicitly derefence.

now all methods that do not involve an assignment on one will be
reflected will be reflected on the other

Try the following in irb

astring = “this is a string object”
bstring = astring

or

bstring = astring = “” or bstring = astring = String.new
astring << “this is a string object”

astring.object_id
bstring.object_id will show that both variables point to the
same object

now all methods invoked that do not involve an assignment on one will
be reflected on the other

for example:

bstring.capitalize! # both bstring and astring are identical

if you
bstring = “this is a new string object”
bstring.object_id shows that bstring points to a different object
and astring and bstring are no longer the same

the same would have happened if you ref = [ 1, 2, 3 ] now ref and a
are different objects

2006/7/8, Phillip H. [email protected]:

If this isn’t appropriate you’ll have to use a container as you are at
the moment.

I’d really like to know what problem Russel is trying to solve. I
never felt the need for this in Ruby - maybe he’s still thinking a tad
too much the Perl way. Russel, any concrete problem you are trying to
solve so we can come up with more concrete suggestions?

Kind regards

robert

Robert K. wrote:

2006/7/8, Phillip H. [email protected]:

If this isn’t appropriate you’ll have to use a container as you are at
the moment.

I’d really like to know what problem Russel is trying to solve. I
never felt the need for this in Ruby - maybe he’s still thinking a tad
too much the Perl way. Russel, any concrete problem you are trying to
solve so we can come up with more concrete suggestions?

the suggestions by other posters will work fine. Defining a container
class is the obvious way to do it – I’m an experienced programmer but a
Ruby newbie – I’m still getting my head around the subleties :slight_smile:

If you are interested in what I am doing see my post on “anonymous
classes and accessors”. I wrote it last night but somehow bungled
posting it to the forum so I will redo it this morning.

Thanks to all who responded your input is appreciated.

To the world at large: I really enjoying playing with Ruby – I’ve got a
couple of weeks off work (Information Security Officer at a large
university) so I have time to do some programming. Hopefully you will
see the results up on Rubyforge before too long. I am working on yet
another program for scanning and managing syslog data - my system’s
claim to fame is that it will handle real time and periodic scanning as
well as long term mantainence with all the configuration data stored in
one place in an easy to understand configuration file. Being written in
Ruby it is designed to be easily extensible – if you want to do some
really clever stuff that I don’t support you will be able to write a
class and have the log records passed to it. The working name is SELMS
– Simple Extensible Log Management System – if anyone thinks of a
better name let me know :slight_smile:

Russell

Russell F. [email protected] writes:

To the world at large: I really enjoying playing with Ruby – I’ve got a
couple of weeks off work (Information Security Officer at a large
university) so I have time to do some programming. Hopefully you will
see the results up on Rubyforge before too long. I am working on yet
another program for scanning and managing syslog data - my system’s
claim to fame is that it will handle real time and periodic scanning as
well as long term mantainence with all the configuration data stored in
one place in an easy to understand configuration file.

I’d love to see that–we’re (a) evaluating Ruby as an “approved”
scripting language where I work, and (b) are not super-happy with Red
Hat’s defafult logwatch setup (our machine build scripts have to patch
the shipped scripts because they let too much noise through, and I
hate doing that).

-Doug

On Sat, 8 Jul 2006, Russell F. wrote:

leaves a[0] == ‘x’
but I can not find anything about explicitly dereferencing a variable.

what I have done in the mean time is to use an array and only use the
first element.

Cheers, Russell

why wouldn’t use simply use an object?

class Container
attr_accessor ‘x’
end

c = Container.new

c.x = 42
p c.x #=> 42

??

-a

Robert K. wrote:

Still it might not be a good solution. There are already reference
types (look for Delegator, SimpleDelegator and WeakReference). That’s
why I asked what concrete problem you are trying to solve. It seemed to
me that you want to transfer a concept that works good in Perl to Ruby
where it does not fit that well. So there might be a better solution.

Absolutely and I’m very open to any suggestions :slight_smile: I’ve just posted a
sizable chunk of code to the forum which illustrates what I’m doing in
the context of a different problem. As I say in that post I’m a Ruby
newbie and still trying to get my head around the Ruby way.

Russell

Russell F. wrote:

the suggestions by other posters will work fine. Defining a container
class is the obvious way to do it – I’m an experienced programmer but a
Ruby newbie – I’m still getting my head around the subleties :slight_smile:

Still it might not be a good solution. There are already reference
types (look for Delegator, SimpleDelegator and WeakReference). That’s
why I asked what concrete problem you are trying to solve. It seemed to
me that you want to transfer a concept that works good in Perl to Ruby
where it does not fit that well. So there might be a better solution.

Kind regards

robert

fr russell:

…it will handle real time and periodic scanning as

well as long term mantainence with all the configuration data

stored in one place in an easy to understand configuration file.

Being

written in Ruby it is designed to be easily extensible – if you want

to do

some really clever stuff that I don’t support you will be able to

write a

class and have the log records passed to it. The working

name is SELMS – Simple Extensible Log Management System –

i’d hope it will also cater for windows platforms. I have problems
managing/monitoring eventlogs (win32 eventlog is good but unstalble yet.
it segfaults for no reason).

hoping to see your project.
kind regards -botp

On 7/8/06, bbiker [email protected] wrote:

In Ruby, all variables (except for number objects) are references and
there is no need to implicitly derefence.

Actually, this isn’t quite true. I’m on vacation right now, but
all variables hold references to objects. It just happens that some
objects are immediate and immutable. Thinking of numbers as
“differently held” will lead you down the wrong way. What makes them
different is not that variables “hold” them differently, but they have
immediate/immutable values. You can make other objects that act very
similarly in pure Ruby. (Just #freeze the object.)

-austin