Pointer concept in Ruby ?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

SGksIEknZCBsaWtlIHRvIGhhdmUgYSB2YXJpYWJsZSBwb2ludGluZyB0byBhIEhhc2gvQXJyYXkg
ZWxlbWVudCwgc28KaWYgSSBtb2RpZnkgdGhlIHZhcmlhYmxlIHRoZW4gdGhlIEhhc2gvQXJyYXkg
ZWxlbWVudCBpcyBtb2RpZmllZCB0b28uCgpBRkFJSyB0aGlzIGlzIG5vdCBwb3NzaWJsZSB3aXRo
IFJ1Ynkgc2luY2UgdGhlIGFic2NlbnNlIG9mIHBvaW50ZXIKY29uY2VwdC4gSSB3YW50IHRoZSBm
b2xsb3dpbmc6CgogIGFycmF5ID0gWyAiYWFhIiwgImJiYiIsICJjY2MiIF0KCiAgYTEgPSBhcnJh
eVsxXS5fc29tZV9tZXRob2RfCiAgPT4gImJiYiIKCiAgYTEgPSAiQkJCIgogID0+ICJCQkIiCgog
IGFbMV0KICA9PiAiQkJCIgoKSXMgaXQgcG9zc2libGUgaW4gc29tZSB3YXk/IFRoYW5rcyBhIGxv
dC4KCgotLSAKScOxYWtpIEJheiBDYXN0aWxsbwo8aWJjQGFsaWF4Lm5ldD4K
 
M

Mikael Høilund

Hi, I'd like to have a variable pointing to a Hash/Array element, so
if I modify the variable then the Hash/Array element is modified too.

AFAIK this is not possible with Ruby since the abscense of pointer
concept. I want the following:

array =3D [ "aaa", "bbb", "ccc" ]

a1 =3D array[1]._some_method_
=3D> "bbb"

a1 =3D "BBB"
=3D> "BBB"

a[1]
=3D> "BBB"

Is it possible in some way? Thanks a lot.


-- =20
I=F1aki Baz Castillo
<[email protected]>

Quoth irb:
array =3D [ "aaa", "bbb", "ccc" ] =3D> ["aaa", "bbb", "ccc"]
a1 =3D array[1] =3D> "bbb"
a1.upcase! =3D> "BBB"
array =3D> ["aaa", "BBB", "ccc"]

You never pass by value, so you're in essence always dealing with =20
pointers.
With your example, you merely made a1 point at a different object, a =20
new string, instead of the second string in the array.

If you however call a self-modifying method on a1 while it's pointing =20=

at the array string, it'll also change in the array, as it's still =20
pointing at the same object.

What are you trying to achieve in practice?=
 
B

Bill Kelly

From: "Iñaki Baz Castillo said:
Hi, I'd like to have a variable pointing to a Hash/Array element, so
if I modify the variable then the Hash/Array element is modified too.

AFAIK this is not possible with Ruby since the abscense of pointer
concept. I want the following:

array = [ "aaa", "bbb", "ccc" ]

a1 = array[1]._some_method_
=> "bbb"

a1 = "BBB"
=> "BBB"

a[1]
=> "BBB"

Is it possible in some way? Thanks a lot.

In general this not easily possible with Ruby, and you'll probably want to
rethink your design so that you don't need to do that.

However, there is a String#replace method, which may be useful if you're
sure this is what you want to do:

=> ["aaa", "bbb", "ccc"]
a1 = array[1] => "bbb"
a1.replace "BBB" => "BBB"
array[1]
=> "BBB"


Regards,

Bill
 
I

Iñaki Baz Castillo

MjAwOC80LzI0LCBJw7Fha2kgQmF6IENhc3RpbGxvIDxpYmNAYWxpYXgubmV0PjoKPiBObywgdGhh
dCdzIG5vdCB0cnVlLiBEZW1vc3RyYXRpb246Cj4KPiAgYXJyYXk9WzAwMCwxMTEsMjIyLDMzXQo+
ICA9PiBbMCwgMTExLCAyMjIsIDMzXQoKU29ycnksIGl0IHdhczoKCmE9WzAsMSwyLDNdCgoKPiAg
YTE9YVsxXQo+ICA9PiAxCj4KPiAgYVsxXS5vYmplY3RfaWQKPiAgPT4gMzQ1Cj4KPiAgYTEub2Jq
ZWN0X2lkCj4gID0+IDM0NQo+Cj4gIGExPSJORVcgVkFMVUUiCj4gID0+ICJORVcgVkFMVUUiCj4K
PiAgPiBhMS5vYmplY3RfaWQKPiAgPT4gLTYwNjA4MTA5OCAgIDwtLS0tLS0tLS0tLSBIYXMgY2hh
bmdlZCAhISEKPgo+ICBhWzFdLm9iamVjdF9pZCAgIDwtLS0tLS0tLSBSZW1haW5zIHRoZSBzYW1l
Cj4gID0+IDM0NQo+Cj4gIGExCj4gID0+ICJORVcgVkFMVUUiCj4KPiAgY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[2]
=3D> "QWE"

There are probabaly a million reasons this is a bad idea, but what about:

require 'ptrclass'
=3D> true
a =3D [0,1,2,3]
=3D> [0, 1, 2, 3]
ap =3D a.ptr(3)
=3D> 3
ap.val=3D4
=3D> 4
a
=3D> [0, 1, 2, 4]
r=3Da.ptr(6)
=3D> nil
r.val=3D4
=3D> 4
a
=3D> [1, 34, 3, nil, nil, nil, 4]

--- ptrclass.rb----
class PtrClass < DelegateClass(Object)
def initialize v
super
__setobj__ v
end
def val=3D v
__setobj__ v
end
end
class Array
def ptr n
v=3D self[n]
self[n] =3D v.is_a?(PtrClass) ? v : PtrClass.new(v)
end
end
 
I

Iñaki Baz Castillo

El Jueves, 24 de Abril de 2008, (e-mail address removed) escribi=C3=B3:
In your example, you weren't changing what a1 pointed to; you were not
changing anything about the object itself.

irb(main):001:0> a =3D ['0','1','2','3']
=3D> ["0", "1", "2", "3"]
irb(main):002:0> a[1]
=3D> "1"
irb(main):003:0> a1 =3D a[1]
=3D> "1"
irb(main):004:0> a1 << 'abc'
=3D> "1abc"
irb(main):005:0> a[1]
=3D> "1abc"

This is what he meant by "self-modifying" methods. =C2=A0Methods that cha= nge
something about the object itself.

Really thanks. You are right, I didn't understand the meaning=20
of "self-modifying" method, I thought that "=3D" was also a "self-modifying=
"=20
method.

Sorry for the confusion and thanks a lot.

=2D-=20
I=C3=B1aki Baz Castillo
 
I

Iñaki Baz Castillo

El Jueves, 24 de Abril de 2008, Adam Shelly escribi=F3:
There are probabaly a million reasons this is a bad idea, but what about:

require 'ptrclass'
=3D> true
a =3D [0,1,2,3]
=3D> [0, 1, 2, 3]
ap =3D a.ptr(3)
=3D> 3
ap.val=3D4
=3D> 4
a
=3D> [0, 1, 2, 4]
r=3Da.ptr(6)
=3D> nil
r.val=3D4
=3D> 4
a
=3D> [1, 34, 3, nil, nil, nil, 4]

--- ptrclass.rb----
class PtrClass < DelegateClass(Object)
def initialize v
super
__setobj__ v
end
def val=3D v
__setobj__ v
end
end
class Array
def ptr n
v=3D self[n]
self[n] =3D v.is_a?(PtrClass) ? v : PtrClass.new(v)
end
end

It seems magic, thanks a lot.


=2D-=20
I=F1aki Baz Castillo
 
S

Sebastian Hungerecker

I=C3=B1aki Baz Castillo said:
I thought that "=3D" was also a "self-modifying"
method.

=3D isn't a method. If it was a method, x=3Dy could not result in x having =
a=20
different object_id than before. It could also not result in x having a=20
different class than before.
What x =3D y does, is that it makes x point to the same object as y which=20
doesn't affect the old object x pointed to at all.
Basically methods change the object (or not) and =3D changes the variable.
I don't think it's possible to have a =3D method without static typing.

Note though that in "foo.bar =3D baz" bar=3D actually is a method on foo an=
d not=20
an assignment operation.

HTH,
Sebastian
=2D-=20
NP: Depeche Mode - Little 15
Jabber: (e-mail address removed)
ICQ: 205544826
 
I

Iñaki Baz Castillo

MjAwOC80LzI1LCBTZWJhc3RpYW4gSHVuZ2VyZWNrZXIgPHNlcHAya0Bnb29nbGVtYWlsLmNvbT46
Cj4gScOxYWtpIEJheiBDYXN0aWxsbyB3cm90ZToKPiAgPiBJIHRob3VnaHQgdGhhdCAiPSIgd2Fz
IGFsc28gYSAic2VsZi1tb2RpZnlpbmciCj4gID4gbWV0aG9kLgo+Cj4KPiA9IGlzbid0IGEgbWV0
aG9kLiBJZiBpdCB3YXMgYSBtZXRob2QsIHg9eSBjb3VsZCBub3QgcmVzdWx0IGluIHggaGF2aW5n
IGEKPiAgZGlmZmVyZW50IG9iamVjdF9pZCB0aGFuIGJlZm9yZS4gSXQgY291bGQgYWxzbyBub3Qg
cmVzdWx0IGluIHggaGF2aW5nIGEKPiAgZGlmZmVyZW50IGNsYXNzIHRoYW4gYmVmb3JlLgo+ICBX
aGF0IHggPSB5IGRvZXMsIGlzIHRoYXQgaXQgbWFrZXMgeCBwb2ludCB0byB0aGUgc2FtZSBvYmpl
Y3QgYXMgeSB3aGljaAo+ICBkb2Vzbid0IGFmZmVjdCB0aGUgb2xkIG9iamVjdCB4IHBvaW50ZWQg
dG8gYXQgYWxsLgo+ICBCYXNpY2FsbHkgbWV0aG9kcyBjaGFuZ2UgdGhlIG9iamVjdCAob3Igbm90
KSBhbmQgPSBjaGFuZ2VzIHRoZSB2YXJpYWJsZS4KPiAgSSBkb24ndCB0aGluayBpdCdzIHBvc3Np
YmxlIHRvIGhhdmUgYSA9IG1ldGhvZCB3aXRob3V0IHN0YXRpYyB0eXBpbmcuCj4KPiAgTm90ZSB0
aG91Z2ggdGhhdCBpbiAiZm9vLmJhciA9IGJheiIgYmFyPSBhY3R1YWxseSBpcyBhIG1ldGhvZCBv
biBmb28gYW5kIG5vdAo+ICBhbiBhc3NpZ25tZW50IG9wZXJhdGlvbi4KClRoYW5rcyBmb3IgdGhl
IGV4cGxhbmF0aW9uLgoKSSB0cnllZCBkb2luZzoKCiAgYTIub2JqZWN0X2lkID0gYXJyYXlbMl0u
b2JqZWN0X2lkCgpidXQgb2J2aW91c2x5IGlzIG5vdCBwZXJtaXR0ZWQgWERERAoKCgotLSAKScOx
YWtpIEJheiBDYXN0aWxsbwo8aWJjQGFsaWF4Lm5ldD4K
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top