[#7500] Re: how to introduce reference objects into ruby — "Geert Fannes" <Geert.Fannes@...>

The problem with the code you sent is that you have to go through ALL

16 messages 2006/03/10

[#7553] "not" operator used in expression that is a method parameter can generate syntax error — noreply@...

Bugs item #3843, was opened at 2006-03-15 22:09

27 messages 2006/03/16
[#7554] Re: [ ruby-Bugs-3843 ] "not" operator used in expression that is a method parameter can generate syntax error — nobu@... 2006/03/16

Hi,

[#7557] Re: [ ruby-Bugs-3843 ] "not" operator used in expression that is a method parameter can generate syntax error — 卜部昌平 <shyouhei@...> 2006/03/16

Nobu, you are not answering to the question.... You have to unveil why

[#7559] Re: [ ruby-Bugs-3843 ] "not" operator used in expression that is a method parameter can generate syntax error — Yukihiro Matsumoto <matz@...> 2006/03/16

Hi,

[#7560] Rant about keyword logical operators was : (Re: [ ruby-Bugs-3843 ] "not" operator used in expression that is a method parameter can generate syntax error) — "Zev Blut" <rubyzbibd@...> 2006/03/16

Hello,

[#7565] Re: [ ruby-Bugs-3843 ] "not" operator used in expression that is a method parameter can generate syntax error — mathew <meta@...> 2006/03/16

Yukihiro Matsumoto wrote:

[#7566] Re: [ ruby-Bugs-3843 ] "not" operator used in expression that is a method parameter can generate syntax error — "Brian Mitchell" <binary42@...> 2006/03/16

On 3/16/06, mathew <meta@pobox.com> wrote:

[#7567] Re: [ ruby-Bugs-3843 ] "not" operator used in expression that is a method parameter can generate syntax error — mathew <meta@...> 2006/03/16

Brian Mitchell wrote:

[#7568] Re: [ ruby-Bugs-3843 ] "not" operator used in expression that is a method parameter can generate syntax error — "Brian Mitchell" <binary42@...> 2006/03/16

On 3/16/06, mathew <meta@pobox.com> wrote:

[#7614] PATCH: A subclassable Pathname — "Evan Phoenix" <evanwebb@...>

A simply change (changing all references of "Pathname.new" to

19 messages 2006/03/27
[#7618] Re: PATCH: A subclassable Pathname — Tanaka Akira <akr@...17n.org> 2006/03/27

In article <92f5f81d0603262350k796fe48fp2224b9f2108ac507@mail.gmail.com>,

[#7619] Re: PATCH: A subclassable Pathname — "Evan Phoenix" <evan@...> 2006/03/27

Quite right on the .glob and .getwd. I guess the tests don't test hit

[#7620] Re: PATCH: A subclassable Pathname — Tanaka Akira <akr@...17n.org> 2006/03/27

In article <92f5f81d0603270903g2fb02244i6a395be708dfffa3@mail.gmail.com>,

how to introduce reference objects into ruby

From: "Geert Fannes" <Geert.Fannes@...>
Date: 2006-03-09 10:47:10 UTC
List: ruby-core #7493
Hello,

 

One thing that hinders the usability of Ruby for the things I want to
do, is the fact that certain objects cannot be modified "in place" and
always return NEW objects (like Fixnum, Bignum or Float), making it hard
to do parameter passing by reference or similar things. Things get
easily out of sync and both memory and cpu time (and programming
overhead) has to be wasted to keep things going as should.

 

To make things clear: Suppose I have a certain number distributed over
different objects and I want this number to be synchronized (read "the
same") in these different places:

 

a=b=12 #a and b are my to objects I want to be synchronized

a+=1

p a #=> 13

p b #=> 12 KO :-( I want it to be 13, exactly the same as a

 

I can of course capture this number in a class, and distribute the class
object instead of the number object:

 

class Bla

  attr :number,true

end

(a=b=Bla.new).number=12

a.number+=1

p a.number #=> 13

p b.number #=> 13 OK! :-)

 

But now my code looks more complicated, I have an extra class and have
to remember to use the instance variable .number. This also wastes too
much memory when e.g. implementing a matrix object where I want to have
both an array holding the rows, one for holding the columns and the
actual rows and columns SHARE the same numbers using objects of type
Bla. This way, if I modify a certain element (say element at row i,
column j) by taking the i-th row and modifying the j-th element in that
row, I want to see the change also if I access the same matrix element
by selecting the j-th column and the i-th element from that column.

 

I tried to create a class that contains a certain Ruby object and that
passes all the incoming methods to the contained object, acting as a
kind of reference.

 

class Reference

  attr :containedObject,true

 

  def initialize(obj)

    @containedObject=obj

  end

 

  #handle missing methods and pass them to the contained object

  def method_missing(methodSymbol,*args)

    @containedObject.send(methodSymbol,*args)

  end

 

  #override some general existing methods, to make sure that a Reference
object is as stealth as possible

  def class

    @containedObject.class

  end

  #unfortunately, this fails since assignment cannot be overloaded,
which makes sense, but still ...

  def =(rhs)

    @containedObject=rhs

  end

end

 

Unfortunately, I cannot overload the assignment operator (and some other
operators), so I still cannot work with these objects as if I was
working with a normal number. So my guess is that to implement such a
Reference class, I have to go deeper and dive in the Ruby code itself,
modifying the unoverloadable operators in the parser etc.

 

This seems a bit drastic, if someone knows an easier solution to this
problem, please let me know. If not, are there people willing to help me
with introducing a Reference object in Ruby or have suggestions how this
can be done? I cannot imagine no one encountered the same problems
before.

 

Greetings,

Geert.

In This Thread

Prev Next