[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Why doesn't Ruby have pointers?

Just Another Victim of the Ambient Morality

6/29/2006 11:55:00 PM

I'm kind of embarrassed to ask this but is there a reason why Ruby
doesn't have variables that refer to other variables? I don't think Python
has them, either, so there must be some reason all these "new age" languages
feel that they don't need them but I can't seem to figure out what that is.
While I would actually be hard-pressed to explain why pointers are
essential in C++, I certainly can't do too much programming in that language
without them! So, there's some sort of difference but I can't put my
finger on it.
Can anyone explain this to me?
Thank you...



7 Answers

Gavin Sinclair

6/30/2006 12:19:00 AM

0

Just Another Victim of the Ambient Morality wrote:
> I'm kind of embarrassed to ask this but is there a reason why Ruby
> doesn't have variables that refer to other variables?

Nearly every variable in Ruby is a pointer to an object. Two variables
can point to the same object. The only exceptions are integers, true,
false, and nil, which are all read-only so it doesn't make a
difference.

Ergo, Ruby does have pointers.

Gavin

John W Kennedy

6/30/2006 12:30:00 AM

0

Just Another Victim of the Ambient Morality wrote:
> I'm kind of embarrassed to ask this but is there a reason why Ruby
> doesn't have variables that refer to other variables?

ALL Ruby variables are (more or less) pointers!

class Cell
attr_accessor :next_cell, :stuff
def initialize(x)
@next_cell, @stuff = nil, x
end
end

root = Cell.new('a')
root.next_cell = Cell.new('b')
root.next_cell.next_cell = Cell.new('c')

x = root
while (x != nil) do
puts x.stuff
x = x.next_cell
end

--
John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
-- Charles Williams. "Taliessin through Logres: Prelude"

Barry Kelly

6/30/2006 1:04:00 AM

0

"Gavin Sinclair" <gsinclair@gmail.com> wrote:

> Just Another Victim of the Ambient Morality wrote:
> > I'm kind of embarrassed to ask this but is there a reason why Ruby
> > doesn't have variables that refer to other variables?

(The OP is talking about pointers to variables, not pointers to
objects.)

> Nearly every variable in Ruby is a pointer to an object. Two variables
> can point to the same object. The only exceptions are integers, true,
> false, and nil, which are all read-only so it doesn't make a
> difference.
>
> Ergo, Ruby does have pointers.

But does it support more than one level of indirection? E.g. we all know
Java, C#, Ruby, Python etc. supports this (in C++):

Object* myObject;

But does it support this:

Object** locationOfMyObject = &myObject;

Java, and to the best of my knowledge, Ruby and Python, don't support
this. C# supports it in a limited scope via 'ref' and 'out' parameters,
while the CLR does support it (otherwise it couldn't support C++/CLI).

It isn't a huge big deal; it simply means you need to create wrapper
classes to add the extra level of indirection where the language doesn't
support it: i.e. some kind of ObjectHolder class.

-- Barry

--
http://barrkel.blo...

S Wayne

6/30/2006 6:08:00 AM

0

To be more precise: most modern programming languages eschew direct
pointers because they have lead to so many problems in programming
languages in the past. Null pointers, pointer out of bounds, etc. All
of these are symptoms of big problems with the old languages like C.

There is another big reason we've gotten rid of direct memory access.
New programming languages are what we call 'managed memory' languages.
This means that rather than the programmer having to do things like
allocating memory for large structures (malloc) and then releasing the
memory, the new languages handle creating the necessary memory when new
objects are created, and releasing the memory when the program is no
longer using the objects. The process is usually referred to as Garbage
Collection.

Managed memory languages are probably the biggest improvement to
programmer productivity since the development of structured
programming. The amount of time spent finding pointer errors in C was
as much as 30% of the maintenance time on software projects. With
garbage collectors, that time has been almost completely eliminated.

Java, Perl, Python and Ruby are all examples of managed memory
languages. Which accounts for them being so popular, and also for some
of the improved productivity when using these languages.

joesb.coe9

6/30/2006 6:30:00 AM

0

Just Another Victim of the Ambient Morality wrote:
> I'm kind of embarrassed to ask this but is there a reason why Ruby
> doesn't have variables that refer to other variables? I don't think Python
> has them, either, so there must be some reason all these "new age" languages
> feel that they don't need them but I can't seem to figure out what that is.
> While I would actually be hard-pressed to explain why pointers are
> essential in C++, I certainly can't do too much programming in that language
> without them! So, there's some sort of difference but I can't put my
> finger on it.
> Can anyone explain this to me?
> Thank you...

You cannot do anything complicate with C++ unless you use pointer
because.

1. C++ doesn't have closure, plus You cannot define nested function in
C++.
2. C++ is static type.

Without closure or nested function, if you have a variable and you want
to set up callback to change the value of that variable you have two
choice between.

* If the variable is known outside current method scope, just define a
function to change it's value. (this includes, instance variable, just
substitue global function callback with method callback).

int x;
void change_x_callback(int newval){ x = newval; }
void test(){
pass_callback(&change_x_callback);
}

* If the variable is local function variable you have no other way to
change it than passing pointer of that int.

void test(){
int x = 0;
pass_x_to_change(&x);
}

In ruby the ruby design will change to using closure

def test
x = 0
invoke_this {|new_val| x = new_val}
end

----

On the other hand, object and closure are equivelant. So you could in
theory, wrap any variable you want in object. and pass in. But because
C++ is static typing, it becomes expensive to create a class just to
capture a variable to change.

I don't know much about Boost, but it seems that they enabled
functional programming very well, I assume less pointer is needed in
Boost. But unless Boost become standard library, most use of C++ will
still require use of pointer in place of closure.


The fact the ruby has no pointer will be issue only if all Ruby library
are designed with pointer in mind. But, because it is so easy to pass
object and closure, all Ruby library are made to utilize the power of
duck typing and block.

msoulier

6/30/2006 3:58:00 PM

0

Barry Kelly wrote:
> But does it support more than one level of indirection? E.g. we all know
> Java, C#, Ruby, Python etc. supports this (in C++):
>
> Object* myObject;
>
> But does it support this:
>
> Object** locationOfMyObject = &myObject;

This is mostly to work around the hack that is C/C++ multi-dimensional
arrays. Since the other languages you mention actually have proper
multi-dimensional arrays, it's really not necessary.

If it is, then you can use some kind of container, as you say.

list_of_refs_to_objects = [[obj1, obj2], [obj3, obj4]]

obj1 = list_of_refs_to_objects[0][0]

Better yet, use a class as the container.

Mike

Dumaiu

6/30/2006 4:56:00 PM

0

joesb wrote:
...
>
> I don't know much about Boost, but it seems that they enabled
> functional programming very well, I assume less pointer is needed in
> Boost. But unless Boost become standard library, most use of C++ will
> still require use of pointer in place of closure.
>
...

This is somewhat off-topic, but although my memory is patchy on the
particulars, the C++ STL already has libraries designed with f. p. in
mind, viz., <algorithm> for functionals, binders for currying... .
Boost takes the approach further, with some really staggering
innovations (such as a lambda implementation), at least a selection
wherefrom will be groomed for probable inclusion in a future language
standard.