Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > pointer and other questions

Reply
Thread Tools

pointer and other questions

 
 
Daniel Schoch
Guest
Posts: n/a
 
      02-23-2009
Hi,
I just started with ruby and I understand from reading the documentation
that pointers don't exist. I'm in the process of writing a netlister.
Such a software is usually built using several linked lists. More
precisely, each element of one list contains a pointer to an element of
some other list.
So I was wondering how this can be achieved.

And while I'm at it I also have the following questions which I didn't
find an answer for in the litarature.
1.
@names.each do |name|
I understand what the line is doing, but why is |name| in the pipe?
What's the definiton of |xx| ?
2.
I came across this in an example piece of ruby code:
options[:verbose] = x
I thought this is a hash with key verbose. But Im not sure now, why is
this :verbose and not options["verbose"]?

Thank you for all the help.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      02-23-2009
On 23.02.2009 21:00, Daniel Schoch wrote:
> I just started with ruby and I understand from reading the documentation
> that pointers don't exist. I'm in the process of writing a netlister.
> Such a software is usually built using several linked lists. More
> precisely, each element of one list contains a pointer to an element of
> some other list.
> So I was wondering how this can be achieved.


Try this in IRB for example:

ListElem = Struct.new rev, :next, :data
l1 = ListElem.new
li.data = "foo"
l2 = ListElem.new
l2.prev = l1
l1.next = l2
l2.data = "bar"
p l1

> And while I'm at it I also have the following questions which I didn't
> find an answer for in the litarature.
> 1.
> @names.each do |name|
> I understand what the line is doing, but why is |name| in the pipe?
> What's the definiton of |xx| ?


It's a block parameter - similar bot not identical to a method parameter
(a block can be viewed as an anonymous function).

> 2.
> I came across this in an example piece of ruby code:
> options[:verbose] = x
> I thought this is a hash with key verbose. But Im not sure now, why is
> this :verbose and not options["verbose"]?


Because the author chose to use Symbol over String for keys. This is
often done when the number of keys is known and limited.

The introductory material out there (hint, hint ) can probably
explain this a lot better and more exhaustively.

Cheers

robert
 
Reply With Quote
 
 
 
 
Daniel Schoch
Guest
Posts: n/a
 
      02-23-2009
>
> ListElem = Struct.new rev, :next, :data
> l1 = ListElem.new
> li.data = "foo"
> l2 = ListElem.new
> l2.prev = l1
> l1.next = l2
> l2.data = "bar"
> p l1
>

Ok, that works and solved my problem. Thanks.

I also found this comment in one of the ruby getting started texts.
> Ruby variables hold references to objects and the = operator copies
> references.


Now what about this ?
b = 1
a = b
b = 2
Shouldn't 'a' now contain 2 if the above statement is true? Clearly not
every variable is a reference.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      02-23-2009
On 23.02.2009 21:40, Daniel Schoch wrote:
>> ListElem = Struct.new rev, :next, :data
>> l1 = ListElem.new
>> li.data = "foo"
>> l2 = ListElem.new
>> l2.prev = l1
>> l1.next = l2
>> l2.data = "bar"
>> p l1
>>

> Ok, that works and solved my problem. Thanks.
>
> I also found this comment in one of the ruby getting started texts.
>> Ruby variables hold references to objects and the = operator copies
>> references.

>
> Now what about this ?
> b = 1
> a = b
> b = 2
> Shouldn't 'a' now contain 2 if the above statement is true? Clearly not
> every variable is a reference.


Please think again. It's the same logic as above.

Cheers

robert
 
Reply With Quote
 
Yossef Mendelssohn
Guest
Posts: n/a
 
      02-23-2009
On Feb 23, 2:40=A0pm, Daniel Schoch <(E-Mail Removed)> wrote:
> I also found this comment in one of the ruby getting started texts.
>
> > Ruby variables hold references to objects and the =3D operator copies
> > references.

>
> Now what about this ?
> b =3D 1
> a =3D b
> b =3D 2
> Shouldn't 'a' now contain 2 if the above statement is true? Clearly not
> every variable is a reference.


This isn't entirely the same with numbers as with many other Ruby data
types because Fixnum is immediate. (There's only one instance of 1.)

But to show the same example using a different datatype:

b =3D 'hi' # b is now a reference to a variable containing the string
'hi'
a =3D b # a and b now reference the same variable. Changing one
(using sub! or similar) will change both
b =3D 'hello' # b is now a reference to a different variable containing
the string 'hello', a is still the old reference to 'hi'

--
-yossef

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      02-23-2009
On 23.02.2009 22:09, Yossef Mendelssohn wrote:
> On Feb 23, 2:40 pm, Daniel Schoch <(E-Mail Removed)> wrote:
>> I also found this comment in one of the ruby getting started texts.
>>
>>> Ruby variables hold references to objects and the = operator copies
>>> references.

>> Now what about this ?
>> b = 1
>> a = b
>> b = 2
>> Shouldn't 'a' now contain 2 if the above statement is true? Clearly not
>> every variable is a reference.

>
> This isn't entirely the same with numbers as with many other Ruby data
> types because Fixnum is immediate. (There's only one instance of 1.)


This is irrelevant from a user's point of view. The logic stays the same:

b = 1 # or any other object!
a = b # a now points to the same object, 1 in this case
b = 2 # or any other object! a still points to 1 while b points to 2

Even though it's technical different under the hood - Fixnums blend
totally with other objects with regard to reference handling.

> But to show the same example using a different datatype:
>
> b = 'hi' # b is now a reference to a variable containing the string
> 'hi'
> a = b # a and b now reference the same variable. Changing one
> (using sub! or similar) will change both
> b = 'hello' # b is now a reference to a different variable containing
> the string 'hello', a is still the old reference to 'hi'


As you demonstrated - it's all the same.

Cheers

robert
 
Reply With Quote
 
Yossef Mendelssohn
Guest
Posts: n/a
 
      02-23-2009
On Feb 23, 3:24=A0pm, Robert Klemme <(E-Mail Removed)> wrote:
> This is irrelevant from a user's point of view. =A0The logic stays the sa=

me:
>
> b =3D 1 # or any other object!
> a =3D b # a now points to the same object, 1 in this case
> b =3D 2 # or any other object! a still points to 1 while b points to 2


I only pointed it out because there's no way (that I know of) to show
that a and b point to the same object at one point if a Fixnum is
used. You can use 'hi'.sub!, but there's no 1.succ!

--
-yossef

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      02-23-2009
On 23.02.2009 22:42, Yossef Mendelssohn wrote:
> On Feb 23, 3:24 pm, Robert Klemme <(E-Mail Removed)> wrote:
>> This is irrelevant from a user's point of view. The logic stays the same:
>>
>> b = 1 # or any other object!
>> a = b # a now points to the same object, 1 in this case
>> b = 2 # or any other object! a still points to 1 while b points to 2

>
> I only pointed it out because there's no way (that I know of) to show
> that a and b point to the same object at one point if a Fixnum is
> used. You can use 'hi'.sub!, but there's no 1.succ!


You can use #object_id for that - even for Fixnums.

Cheers

robert
 
Reply With Quote
 
Daniel Schoch
Guest
Posts: n/a
 
      02-23-2009

>
> Please think again. It's the same logic as above.
>



well, well. That's one hell of an explantion. But I guess it works out
ok. It's a bit of a getting used to if one is used to C in daily life.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      02-24-2009
Daniel Schoch wrote:
> I just started with ruby and I understand from reading the documentation
> that pointers don't exist. I'm in the process of writing a netlister.
> Such a software is usually built using several linked lists. More
> precisely, each element of one list contains a pointer to an element of
> some other list.
> So I was wondering how this can be achieved.


In Ruby everything is a reference to an object, and you can think of a
reference as a pointer to that object's representation in memory.

There is an optimisation for Fixnum, nil, false and true which means
that these don't actually allocate memory, because the value is buried
within the reference itself, but from the outside they still behave the
same:

a = 3
puts a # 3

class Fixnum
def double
self + self
end
end

puts a.double # 6

Note that Fixnum/true/false/nil are immutable. That is, you can't change
the value of the object '3', but you can change your variable so that it
holds a reference to some other object.

a = 4 # a now points to a different Fixnum
puts a # 4

For mutable objects, you end up with aliasing effects:

a = "hello"
b = a # pointer to same object
a.upcase!
puts a # "HELLO"
puts b # "HELLO"

The aliasing is the same as you'd get with

char *a = strdup("hello");
char *b = a;

HTH,

Brian.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM
late-binding of name to function pointer and other questions Dennis Chang C Programming 3 05-15-2004 12:01 AM



Advertisments