inplace assignment

Discussion in 'Ruby' started by T. Onoma, Dec 14, 2003.

  1. T. Onoma

    T. Onoma Guest

    is there anyway, anyway at all, ugly hacks accepted, of doing inplace
    assignment in Ruby?
    --
    T.
    T. Onoma, Dec 14, 2003
    #1
    1. Advertising

  2. T. Onoma

    Hal Fulton Guest

    T. Onoma wrote:
    > is there anyway, anyway at all, ugly hacks accepted, of doing inplace
    > assignment in Ruby?


    Not sure what you mean, can you give an example from
    some other language? Or just explain?

    Hal
    Hal Fulton, Dec 14, 2003
    #2
    1. Advertising

  3. T. Onoma

    T. Onoma Guest

    On Sunday 14 December 2003 05:51 am, Hal Fulton wrote:
    > T. Onoma wrote:
    > > is there anyway, anyway at all, ugly hacks accepted, of doing inplace
    > > assignment in Ruby?

    >
    > Not sure what you mean, can you give an example from
    > some other language? Or just explain?
    >
    > Hal


    Sure,

    q = 1
    p q.__id__ # => 3
    q = 2
    p q.__id__ # => 5 (want this to still be 3)

    In other words I want to change what q "contains" rather then alter its
    reference. With an array for example you can do that with #replace. In
    particular I'm interesed in doing this with constants.

    --
    T.
    T. Onoma, Dec 14, 2003
    #3
  4. T. Onoma

    Guest

    Hi,

    At Sun, 14 Dec 2003 15:14:44 +0900,
    T. Onoma wrote:
    > In other words I want to change what q "contains" rather then alter its
    > reference. With an array for example you can do that with #replace. In
    > particular I'm interesed in doing this with constants.


    Array#replace isn't concerned with constants.

    A = [1]
    A.replace([2])
    p A[0] # => 2

    --
    Nobu Nakada
    , Dec 14, 2003
    #4
  5. T. Onoma

    Hal Fulton Guest

    T. Onoma wrote:
    >
    > Sure,
    >
    > q = 1
    > p q.__id__ # => 3
    > q = 2
    > p q.__id__ # => 5 (want this to still be 3)
    >
    > In other words I want to change what q "contains" rather then alter its
    > reference. With an array for example you can do that with #replace. In
    > particular I'm interesed in doing this with constants.
    >


    OK, I thought that was what you meant.

    String also has a replace. But there's no general Object#replace.

    My impression is that this in impossible in general, and for
    immediate values such as Fixnums, "even more impossible."

    The idea of doing this with a constant is scary to me. It's bad
    enough that a constant String or Array can be changed. And it's
    even scarier to think of doing that with something like a
    Fixnum.

    What's the situation where you'd want to do this?

    Hal
    Hal Fulton, Dec 14, 2003
    #5
  6. On Sunday, December 14, 2003, 5:14:44 PM, T. wrote:

    > On Sunday 14 December 2003 05:51 am, Hal Fulton wrote:
    >> T. Onoma wrote:
    >> > is there anyway, anyway at all, ugly hacks accepted, of doing inplace
    >> > assignment in Ruby?

    >>
    >> Not sure what you mean, can you give an example from
    >> some other language? Or just explain?
    >>
    >> Hal


    > Sure,


    > q = 1
    > p q.__id__ # => 3
    > q = 2
    > p q.__id__ # => 5 (want this to still be 3)


    > In other words I want to change what q "contains" rather then alter its
    > reference. With an array for example you can do that with #replace. In
    > particular I'm interesed in doing this with constants.


    For "constants" I presume you mean "integers" here? (Constants can be
    of any class, and simply mean a variable that begins with a capital
    letter, and which are thinly guarded against reassignment.)

    You certainly can't do what you're asking in Ruby. Fixnum values are
    hardcoded objects for performance reasons.

    Fixnums aside, there's nothing you can do to change the behaviour of
    "=".

    Gavin
    Gavin Sinclair, Dec 14, 2003
    #6
  7. T. Onoma

    Hal Fulton Guest

    wrote:
    > Hi,
    >
    > At Sun, 14 Dec 2003 15:14:44 +0900,
    > T. Onoma wrote:
    >
    >>In other words I want to change what q "contains" rather then alter its
    >>reference. With an array for example you can do that with #replace. In
    >>particular I'm interesed in doing this with constants.

    >
    >
    > Array#replace isn't concerned with constants.
    >
    > A = [1]
    > A.replace([2])
    > p A[0] # => 2


    Thanks for pointing this out. I don't think I ever noticed that.

    Should this give an error?

    Hal
    Hal Fulton, Dec 14, 2003
    #7
  8. T. Onoma

    Guest

    Hi,

    At Sun, 14 Dec 2003 15:50:54 +0900,
    Hal Fulton wrote:
    > >>In other words I want to change what q "contains" rather then alter its
    > >>reference. With an array for example you can do that with #replace. In
    > >>particular I'm interesed in doing this with constants.

    > >
    > >
    > > Array#replace isn't concerned with constants.
    > >
    > > A = [1]
    > > A.replace([2])
    > > p A[0] # => 2

    >
    > Thanks for pointing this out. I don't think I ever noticed that.
    >
    > Should this give an error?


    No. A constant in Ruby is a name which can point to only
    particular object. Nothing related to the container's
    contents.

    --
    Nobu Nakada
    , Dec 14, 2003
    #8
  9. T. Onoma

    T. Onoma Guest

    On Sunday 14 December 2003 07:49 am, Hal Fulton wrote:
    > OK, I thought that was what you meant.
    >
    > String also has a replace. But there's no general Object#replace.
    >
    > My impression is that this in impossible in general, and for
    > immediate values such as Fixnums, "even more impossible."
    >
    > The idea of doing this with a constant is scary to me. It's bad
    > enough that a constant String or Array can be changed. And it's
    > even scarier to think of doing that with something like a
    > Fixnum.
    >
    > What's the situation where you'd want to do this?


    Well, the reason its a constant is b/c its a class. It's funny how things come
    up. I only recently learned that when you modify a class, all previously
    defined objects of that class are effected. But what if you want to pull the
    entire "rug out", so to speak, and replace a class with a "duck similiar"
    class?

    Okay, so it's a bit crazy. But I actually came across a use for this. Luckly
    my situation is specialized --I am replacing the class with a subclass of it,
    so I discovered that I could just do this:

    class MyClass < MyClass

    (and now that I think about it I can probably do this for any calss) it seems
    to work fine.

    but now i'm running into an disturbing problem. next post...

    --
    T.
    T. Onoma, Dec 14, 2003
    #9
  10. On Sunday, December 14, 2003, 5:50:54 PM, Hal wrote:

    >>
    >> Array#replace isn't concerned with constants.
    >>
    >> A = [1]
    >> A.replace([2])
    >> p A[0] # => 2


    > Thanks for pointing this out. I don't think I ever noticed that.


    > Should this give an error?


    That's what #freeze is for.

    Gavin
    Gavin Sinclair, Dec 14, 2003
    #10
  11. On Sun, Dec 14, 2003 at 03:49:07PM +0900, Hal Fulton wrote:
    > OK, I thought that was what you meant.
    >
    > String also has a replace. But there's no general Object#replace.
    >
    > My impression is that this in impossible in general, and for
    > immediate values such as Fixnums, "even more impossible."
    >
    > The idea of doing this with a constant is scary to me. It's bad


    Don't you like open classes? ;-)

    > enough that a constant String or Array can be changed. And it's
    > even scarier to think of doing that with something like a
    > Fixnum.


    --
    _ _
    | |__ __ _| |_ ___ _ __ ___ __ _ _ __
    | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
    | |_) | (_| | |_\__ \ | | | | | (_| | | | |
    |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
    Running Debian GNU/Linux Sid (unstable)
    batsman dot geo at yahoo dot com

    /*
    * Please skip to the bottom of this file if you ate lunch recently
    * -- Alan
    */
    -- from Linux kernel pre-2.1.91-1
    Mauricio Fernández, Dec 14, 2003
    #11
  12. T. Onoma

    Dan Doel Guest

    Mauricio Fernández wrote:

    >Don't you like open classes? ;-)
    >
    >


    I think he's more afraid of:

    1 = 2
    p 1 + 1 # => 4

    - Dan
    Dan Doel, Dec 14, 2003
    #12
  13. T. Onoma

    Hal Fulton Guest

    wrote:
    > Hi,
    >
    > At Sun, 14 Dec 2003 15:50:54 +0900,
    > Hal Fulton wrote:
    >
    >>>>In other words I want to change what q "contains" rather then alter its
    >>>>reference. With an array for example you can do that with #replace. In
    >>>>particular I'm interesed in doing this with constants.
    >>>
    >>>
    >>>Array#replace isn't concerned with constants.
    >>>
    >>> A = [1]
    >>> A.replace([2])
    >>> p A[0] # => 2

    >>
    >>Thanks for pointing this out. I don't think I ever noticed that.
    >>
    >>Should this give an error?

    >
    >
    > No. A constant in Ruby is a name which can point to only
    > particular object. Nothing related to the container's
    > contents.
    >


    Actually, I completely misread your example. I already understood this
    phenomenon.

    I thought that the assignment was silently discarded, i.e., I read this
    as:

    A = [1]
    A.replace([2])
    p A[0] # => 1

    Sorry for being stupid. :)

    Hal
    Hal Fulton, Dec 14, 2003
    #13
  14. il Sun, 14 Dec 2003 15:50:54 +0900, Hal Fulton
    <> ha scritto::


    >
    >Thanks for pointing this out. I don't think I ever noticed that.
    >
    >Should this give an error?
    >


    every 'changeable' object, such has String, Hash, Array etc works this
    way. The point is that the object refernce is constant, not the
    object.
    IMO this is a work for freeze() , if you really want an error
    gabriele renzi, Dec 14, 2003
    #14
  15. T. Onoma

    Hal Fulton Guest

    gabriele renzi wrote:
    > every 'changeable' object, such has String, Hash, Array etc works this
    > way. The point is that the object refernce is constant, not the
    > object.
    > IMO this is a work for freeze() , if you really want an error


    Thank you. Yes, I knew this already. My brain was not working properly.

    Thanks,
    Hal
    Hal Fulton, Dec 14, 2003
    #15
  16. "T. Onoma" <> schrieb im Newsbeitrag
    news:...
    > On Sunday 14 December 2003 05:51 am, Hal Fulton wrote:
    > > T. Onoma wrote:
    > > > is there anyway, anyway at all, ugly hacks accepted, of doing

    inplace
    > > > assignment in Ruby?

    > >
    > > Not sure what you mean, can you give an example from
    > > some other language? Or just explain?
    > >
    > > Hal

    >
    > Sure,
    >
    > q = 1
    > p q.__id__ # => 3
    > q = 2
    > p q.__id__ # => 5 (want this to still be 3)
    >
    > In other words I want to change what q "contains" rather then alter its
    > reference. With an array for example you can do that with #replace.


    You want to change the state of the instance at hand and don't want to
    change its identity. It depends on the type of instance at hand whether
    you can achieve your goal: it doesn't work for integers, but you can
    change a String (which also works for constants):

    irb(main):016:0> s = "foo"
    => "foo"
    irb(main):017:0> s.id
    => 135086644
    irb(main):018:0> s = "bar"
    => "bar"
    irb(main):019:0> s.id
    => 135071956
    irb(main):020:0> s.replace "foo"
    => "foo"
    irb(main):021:0> s.id
    => 135071956
    irb(main):022:0>

    There is no general solution to what you want. Personally I never needed
    this feature. Normally you can solve this with proper nesting.

    > In particular I'm interesed in doing this with constants.


    Keep in mind that the constness of constants is all constants are about.
    So changing them is generally not a good idea if it's a simple type like
    String or Integer.

    Regards

    robert
    Robert Klemme, Dec 15, 2003
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.verizon.net

    inplace edit error message

    news.verizon.net, Sep 29, 2003, in forum: Perl
    Replies:
    4
    Views:
    1,590
    Jim Gibson
    Oct 8, 2003
  2. msnews.microsoft.com

    DataGrid InPlace Editing

    msnews.microsoft.com, Jan 29, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    341
    msnews.microsoft.com
    Jan 29, 2005
  3. jason
    Replies:
    0
    Views:
    415
    jason
    Nov 9, 2005
  4. nutty
    Replies:
    6
    Views:
    386
    Tom Widmer
    Jun 15, 2006
  5. Oleg Paraschenko

    Ann: InPlace, a new xslt-based CMS

    Oleg Paraschenko, Oct 4, 2007, in forum: XML
    Replies:
    0
    Views:
    440
    Oleg Paraschenko
    Oct 4, 2007
Loading...

Share This Page