[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Pointer concept in Ruby ?

Iñaki Baz Castillo

4/24/2008 4:01:00 PM

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

11 Answers

Mikael Høilund

4/24/2008 4:08:00 PM

0


On Apr 24, 2008, at 18:01, I=F1aki Baz Castillo wrote:

> 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
> <ibc@aliax.net>

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?=

Bill Kelly

4/24/2008 4:16:00 PM

0


From: "Iñaki Baz Castillo" <ibc@aliax.net>
>
> 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ñaki Baz Castillo

4/24/2008 4:26:00 PM

0

MjAwOC80LzI0LCBJw7Fha2kgQmF6IENhc3RpbGxvIDxpYmNAYWxpYXgubmV0PjoKPiBObywgdGhh
dCdzIG5vdCB0cnVlLiBEZW1vc3RyYXRpb246Cj4KPiAgYXJyYXk9WzAwMCwxMTEsMjIyLDMzXQo+
ICA9PiBbMCwgMTExLCAyMjIsIDMzXQoKU29ycnksIGl0IHdhczoKCmE9WzAsMSwyLDNdCgoKPiAg
YTE9YVsxXQo+ICA9PiAxCj4KPiAgYVsxXS5vYmplY3RfaWQKPiAgPT4gMzQ1Cj4KPiAgYTEub2Jq
ZWN0X2lkCj4gID0+IDM0NQo+Cj4gIGEx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VyZSBhbnkgbWVob3RkIG1vcmUgIm5vcm1hbCI/IHNvbWV0aGluZyBhczoKCmEyID0g
YXJyYXlbMl0uX3NvbWV0aGluZ18KCmEyPSJRV0UiCgphcnJheVsyXQo9PiAiUVdFIgoKClRoYW5r
cyBhIGxvdC4KCi0tIApJw7Fha2kgQmF6IENhc3RpbGxvCjxpYmNAYWxpYXgubmV0Pgo=

Simon Krahnke

4/24/2008 6:23:00 PM

0

* Iñaki Baz Castillo <ibc@aliax.net> (18:24) schrieb:

>> If you however call a self-modifying method on a1 while it's pointing at
>> the array string, it'll also change in the array, as it's still pointing at
>> the same object.
>
> No, that's not true. Demostration:

You aren't calling a "self-modifying method".

mfg, simon .... l

Simon Krahnke

4/24/2008 6:25:00 PM

0

* Iñaki Baz Castillo <ibc@aliax.net> (18:30) schrieb:

> a2 = array[2]._something_
>
> a2="QWE"
>
> array[2]
> => "QWE"

You can't redefine =.

mfg, simon .... l

Adam Shelly

4/24/2008 8:55:00 PM

0

On 4/24/08, I=F1aki Baz Castillo <ibc@aliax.net> wrote:
> Isn't there any mehotd more "normal"? something as:
>
> a2 =3D array[2]._something_
>
> a2=3D"QWE"
>
> array[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

---
-Adam

Iñaki Baz Castillo

4/24/2008 9:23:00 PM

0

El Jueves, 24 de Abril de 2008, khaines@enigo.com 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ñaki Baz Castillo

4/24/2008 9:43:00 PM

0

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

Sebastian Hungerecker

4/25/2008 7:48:00 AM

0

I=C3=B1aki Baz Castillo wrote:
> 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: sepp2k@jabber.org
ICQ: 205544826