Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > inplace assignment

Reply
Thread Tools

inplace assignment

 
 
T. Onoma
Guest
Posts: n/a
 
      12-14-2003
is there anyway, anyway at all, ugly hacks accepted, of doing inplace
assignment in Ruby?
--
T.


 
Reply With Quote
 
 
 
 
Hal Fulton
Guest
Posts: n/a
 
      12-14-2003
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




 
Reply With Quote
 
 
 
 
T. Onoma
Guest
Posts: n/a
 
      12-14-2003
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.

 
Reply With Quote
 
nobu.nokada@softhome.net
Guest
Posts: n/a
 
      12-14-2003
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


 
Reply With Quote
 
Hal Fulton
Guest
Posts: n/a
 
      12-14-2003
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


 
Reply With Quote
 
Gavin Sinclair
Guest
Posts: n/a
 
      12-14-2003
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



 
Reply With Quote
 
Hal Fulton
Guest
Posts: n/a
 
      12-14-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) 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






 
Reply With Quote
 
nobu.nokada@softhome.net
Guest
Posts: n/a
 
      12-14-2003
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


 
Reply With Quote
 
T. Onoma
Guest
Posts: n/a
 
      12-14-2003
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.


 
Reply With Quote
 
Gavin Sinclair
Guest
Posts: n/a
 
      12-14-2003
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





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ann: InPlace, a new xslt-based CMS Oleg Paraschenko XML 0 10-04-2007 04:56 AM
gcc needs copy consturctor for inplace constructed object passed to func as reference, others not nutty C++ 6 06-15-2006 02:02 PM
asp 1.1 datagrid inplace editing, utf-8 code saved as "??" ? jason ASP .Net 0 11-09-2005 06:57 AM
DataGrid InPlace Editing msnews.microsoft.com ASP .Net 0 01-29-2005 10:11 AM
inplace edit error message news.verizon.net Perl 4 10-08-2003 09:43 PM



Advertisments