Pointers in Ruby

J

Jean-Hugues ROBERT

Hi,

I am coining a Pointer class in Ruby. Kind of a C pointer, more
like a C++ smart pointer actually. Should work for any lvalue.

Any previous art ?
Any way to avoid evil eval ?

Yours,

Jean-Hugues

PS: thanks to http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc


class Pointer
attr_accessor :lvalue
attr_reader :binding
attr_reader :getter
attr_reader :setter
def initialize( &block ) ref( &block) end
def ref( &block )
@binding = block.binding()
@lvalue = block.call()
@getter = eval( "proc { #@lvalue }", block)
@setter = eval( "proc { |v| #@lvalue = v }", block)
end
def []() @getter.call() end
def []=( new_value ) @setter.call( new_value); end
def to_s() self[].to_s() end
def inspect() "<Pointer #{lvalue()}>#{self[].inspect()}" end
end

ptr = Pointer.new {"$A"}
ptr[] = "Hello"
ptr[] += ", world."
p "@{$A} -- #{ptr}"
b = nil

ptr.ref{ :b}
ptr[] = "This is Ruby."
p b
p ptr
ptr.ref{ :ENV}
p ENV["USERNAME"]
ptr[]["USERNAME"] = "Ruby"
p ENV["USERNAME"]
p ptr.binding()
 
R

Robert Klemme

Jean-Hugues ROBERT said:
Hi,

I am coining a Pointer class in Ruby. Kind of a C pointer, more
like a C++ smart pointer actually. Should work for any lvalue.

Care to share what usage you envision for this?

Btw: this rather looks like a reference than a pointer. As far as I
understand, you create an alias for some variable (possibly a local var)
as a handle to modify it from outside the original scope. Is that
correct?

robert
 
D

David Alan Black

Hi --

Jean-Hugues ROBERT said:
Hi,

I am coining a Pointer class in Ruby. Kind of a C pointer, more
like a C++ smart pointer actually. Should work for any lvalue.

Any previous art ?

Well, there's the fact that all variables in Ruby are already
references :) So you can do things like:

e = ENV
e["USERNAME"] = "Ruby"
p ENV["USERNAME"] # "Ruby"

Can you give an example of something you would need the extra layer
of reference for?


David
 
G

Gregory Millam

Received: Thu, 29 Apr 2004 21:59:03 +0900
Can you give an example of something you would need the extra layer
of reference for?

I'd imagine Fixnums and other immutable objects. But writing a wrapper class is easy enough, without a pointer class. Perhaps the OP can see a reason for such a pointer where we can't.
 
D

David Alan Black

Hi --

Gregory Millam said:
Received: Thu, 29 Apr 2004 21:59:03 +0900


I'd imagine Fixnums and other immutable objects. But writing a
wrapper class is easy enough, without a pointer class. Perhaps the
OP can see a reason for such a pointer where we can't.

Yes -- hence my question :)


David
 
J

Jean-Hugues ROBERT

Care to share what usage you envision for this?

Btw: this rather looks like a reference than a pointer. As far as I
understand, you create an alias for some variable (possibly a local var)
as a handle to modify it from outside the original scope. Is that
correct?

robert

Correct. But it should works for any lvalue not just variables.
i.e. ref{"a_hash[3]"} for example.


I initially named it a reference. But, references are usually automatically
dereferenced (they are more transparent than pointers).

I could no find a way to do this in Ruby. That is why I am using the
[]() accessor to get the referenced value. This is very similar to
what you do in C with a pointer using * (content of). So, I eventually
renamed the class Pointer.

Now, I may be wrong about the difference between a pointer and a reference
and I would be happy to change the code to something better.

Yours,

Jean-Hugues
 
J

Jean-Hugues ROBERT

Hi --

Jean-Hugues ROBERT said:
Hi,

I am coining a Pointer class in Ruby. Kind of a C pointer, more
like a C++ smart pointer actually. Should work for any lvalue.

Any previous art ?

Well, there's the fact that all variables in Ruby are already
references :) So you can do things like:

e = ENV
e["USERNAME"] = "Ruby"
p ENV["USERNAME"] # "Ruby"

Can you give an example of something you would need the extra layer
of reference for?


David

"Any problem in computer science can be solved with another level
of indirection." - David Wheeler.
See http://c2.com/cgi/wiki?ButlerLampson

I need that Pointer class to implement a more complex one called
LogicVariable. A logic variable potentially references a
regular variable and provide addition behavior. Mainly a "match(a,b)"
method. Match does Prolog style pattern matching. A LogicVariable
can be either bound to some value, or free. When it is free it
acts as a wild card and will be assigned whatever value matches
in the other parameter of match.

Example:
def test()

a = [[1],"z"]
b = 0
logic_b = LogicVariable.new( ref{":b"})
logic_b.free!
match( a, [[logic_v],Free])
p b # => 1

I need the pointer to assign b as a side effect of match().

lvalues are one of the few things that are not objects in Ruby
(yet), which is why I needed a special class to manipulate them.

Yours,

Jean-Hugues
 
R

Robert Klemme

Jean-Hugues ROBERT said:
Care to share what usage you envision for this?

Btw: this rather looks like a reference than a pointer. As far as I
understand, you create an alias for some variable (possibly a local var)
as a handle to modify it from outside the original scope. Is that
correct?

robert

Correct. But it should works for any lvalue not just variables.
i.e. ref{"a_hash[3]"} for example.


I initially named it a reference. But, references are usually automatically
dereferenced (they are more transparent than pointers).

I could no find a way to do this in Ruby. That is why I am using the
[]() accessor to get the referenced value.

I'd simply use ordinary methods in the absence of an appropriate operator
that you can use.

class Pointer
def deref() ...
def deref=(x) ...
end

That's much more readable although a bit more typing. IMHO using [] makes
it unnecessary difficult since it raises other associations.
This is very similar to
what you do in C with a pointer using * (content of). So, I eventually
renamed the class Pointer.

I guess you mean "SomeType& operator*() const" etc. in C++.
Now, I may be wrong about the difference between a pointer and a reference
and I would be happy to change the code to something better.

For the scenario you describe in the other posting I'd rather choose
another approach, like holding all values in a hash and using symbols to
reference them. That way, you can manipulate them easier IMHO. But that
might be a matter of taste and of course I don't now other preconditions
of your app...

Example:

class Var
def initialize( env, sym )
@env, @sym = env, sym
end

def val; @env[@sym]; end
def val=(x); @env[@sym] = x; end
end

env = { :x => true, :y => false }

x = Var.new(env, :x)
p x
p x.val

x.val = !x.val
p x
p x.val

Your class LogicVar might even inherit this class. Or whatever is most
appropriate in your situation.

Regards

robert
 
H

Hal Fulton

Jean-Hugues ROBERT said:
"Any problem in computer science can be solved with another level
of indirection." - David Wheeler.

I may easily be wrong, but the way I have heard this quote is:
"Computer science is the discipline that THINKS every problem can be
solved with an additional level of indirection." (emphasis mine)

That is different, because it is subtly making fun of this tendency
in our field, and is actually making the point that this is NOT
true...


Cheers,
Hal
 
J

Jean-Hugues ROBERT

I may easily be wrong, but the way I have heard this quote is:
"Computer science is the discipline that THINKS every problem can be
solved with an additional level of indirection." (emphasis mine)

That is different, because it is subtly making fun of this tendency
in our field, and is actually making the point that this is NOT
true...


Cheers,
Hal

I take it the other way: Only a problem that can be solved with
another level of indirection is solvable by a computer.

If you prefer: A computer is something that can solve problems,
as long as the problem is solvable using another level of indirection.

Now, how are you going to get by with that dinner you promized to cook ?

But of course I believe that Artificial Intelligence is more
artificial than intelligent.

I like the statement because it rephrases in more simple terms
what is a Turing complete machine and what it can do (and what
it can't !).

I knew that Ruby was about philosophy ;-)

Yours,

Jean-Hugues
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top