Professional Web Applications Themes

ruby and pointers - Ruby

Hello! Suppose: pool = [1.2, 2.434, 1.132, 1.1334] foo = 0.0 pool.each do |x| foo = x # foo changes value and actual address in mem end Can I give foo the value *only* of x but not change its actual address in memory? Regards, -- University of Athens I bet the human brain Physics Department is a kludge --Marvin Minsky...

  1. #1

    Default ruby and pointers

    Hello!

    Suppose:

    pool = [1.2, 2.434, 1.132, 1.1334]
    foo = 0.0
    pool.each do |x|
    foo = x # foo changes value and actual address in mem
    end

    Can I give foo the value *only* of x but not change its
    actual address in memory?

    Regards,
    --
    University of Athens I bet the human brain
    Physics Department is a kludge --Marvin Minsky




    Elias Guest

  2. #2

    Default Re: ruby and pointers

    Elias Athanasopoulos wrote:
     

    I don't think so, because foo is a reference to a Float object, and
    Float objects are immutable. In contrast, if foo were a reference to
    some kind of mutable object (like a String), you *could* change its
    value without creating a new object in the process, e.g.

    foo = "a string"
    puts foo.id
    foo.sub!("a", "the")
    puts foo.id # should be the same as before the sub!

    Hope this helps,

    Lyle
    Lyle Guest

  3. #3

    Default Re: ruby and pointers

    Elias Athanasopoulos wrote: 

    Variables in Ruby are not objects, they hold only references to other
    objects. At first foo references object 0.0 (Float), then 1.2 and so on.

    Gennady.


    Gennady Guest

  4. #4

    Default Re: ruby and pointers

    On Tue, Feb 03, 2004 at 02:42:35PM -0600, Lyle Johnson wrote: 
    >
    > I don't think so, because foo is a reference to a Float object, and
    > Float objects are immutable. In contrast, if foo were a reference to[/ref]

    Thanks Lyle for the quick reply. What I wanted to do was
    something I could do in C like:

    RFLOAT(foo)->value = RFLOAT(x)->value;

    I thought if Ruby could do this ad hoc for Float objects.

    Regards,
    --
    University of Athens I bet the human brain
    Physics Department is a kludge --Marvin Minsky





    Elias Guest

  5. #5

    Default Re: ruby and pointers

    Is there a specific reason of why you want to do that?

    Guillaume.

    On Tue, 2004-02-03 at 15:51, Elias Athanasopoulos wrote: 
    > >
    > > I don't think so, because foo is a reference to a Float object, and
    > > Float objects are immutable. In contrast, if foo were a reference to[/ref]
    >
    > Thanks Lyle for the quick reply. What I wanted to do was
    > something I could do in C like:
    >
    > RFLOAT(foo)->value = RFLOAT(x)->value;
    >
    > I thought if Ruby could do this ad hoc for Float objects.
    >
    > Regards,
    > --
    > University of Athens I bet the human brain
    > Physics Department is a kludge --Marvin Minsky
    >
    >
    >
    >[/ref]




    Guillaume Guest

  6. #6

    Default Re: ruby and pointers

    Hello!

    On Wed, Feb 04, 2004 at 08:52:43AM +0900, Guillaume Marcais wrote: [/ref]

    Yes, a lazy way to port this kind of behaviour:

    int foo;
    Bar *b = new Bar(&foo);
    for (int i; i < 10; i++)
    {
    foo = i;
    b->collect();
    }

    b->dump();

    ANW, I found how to accomplish such a thing, but it would
    be easier if Ruby supported it by nature.

    Regards,
    --
    University of Athens I bet the human brain
    Physics Department is a kludge --Marvin Minsky




    Elias Guest

Similar Threads

  1. Pointers on how to do the following
    By Jay in forum PHP Development
    Replies: 2
    Last Post: January 10th, 10:21 PM
  2. Ruby and Swig -- creating Pointers (typedef)
    By RubyQuestions in forum Ruby
    Replies: 4
    Last Post: January 13th, 09:46 PM
  3. Pointers and References
    By Jason Dusek in forum PERL Beginners
    Replies: 2
    Last Post: November 30th, 09:34 PM
  4. Pointers
    By Eric Walker in forum PERL Beginners
    Replies: 11
    Last Post: November 22nd, 04:10 PM
  5. [ANN] ruby-freedb, ruby-serialport, ruby-mp3info moved to Rubyforge
    By guillaume.pierronnet@ratp.fr in forum Ruby
    Replies: 0
    Last Post: August 31st, 11:57 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139