is there anyway, anyway at all, ugly hacks accepted, of doing inplace
assignment in Ruby?
···
–
T.
is there anyway, anyway at all, ugly hacks accepted, of doing inplace
assignment in Ruby?
–
T.
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.
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
–
T.
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
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
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
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.
“T. Onoma” transami@runbox.com schrieb im Newsbeitrag
news:200312132208.22847.transami@runbox.com…
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
On Sunday 14 December 2003 05:51 am, Hal Fulton wrote:
Thanks for pointing this out. I don’t think I ever noticed that.
Should this give an error?
Hal
nobu.nokada@softhome.net 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
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…
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?
–
T.
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?
On Sun, Dec 14, 2003 at 03:49:07PM +0900, Hal Fulton wrote:
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
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] # => 2Thanks 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
That’s what #freeze is for.
Gavin
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?
Mauricio Fernández wrote:
Don’t you like open classes?
I think he’s more afraid of:
1 = 2
p 1 + 1 # => 4
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?
s = String # now it’s a variable
As for modifying a class: the objects are affected in the sense that
the methods in their lookup path have changed. They themselves are
not changed at all.
I’m not sure what you mean by “duck similar” (the notion of duck
typing doesn’t scale well to “duck ”), but assuming you
mean a class that defines methods with the same names and arities as
another class, the best thing would probably be to do:
module A; # bunch of methods; end
module B; # same-named methods as A; end
class C; include A; end
class C; include B; end # when you want the new ones
or
class C; include A; end
c = C.new
obj.extend(B)
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
Yikes. I think that resorting to that is a reasonably good
indication that you might want to look for a new program design
David
On Sun, 14 Dec 2003, T. Onoma wrote:
On Sunday 14 December 2003 07:49 am, Hal Fulton wrote:
–
David A. Black
dblack@wobblini.net
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
il Sun, 14 Dec 2003 15:50:54 +0900, Hal Fulton hal9000@hypermetrics.com ha scritto::
Thanks for pointing this out. I don’t think I ever noticed that.
Should this give an error?
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
nobu.nokada@softhome.net 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] # => 2Thanks 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.
irb(main):001:0> class A < A
irb(main):002:1> def test
irb(main):003:2> super
irb(main):004:2> end
irb(main):005:1> end
TypeError: undefined superclass `A’
from (irb):1
irb(main):006:0>
Special way of writing recursive methods/classes, which reminds me of
modern c++ design’s powerful ideas of recursive classes.
Is it possible to make classes which inherit from themselves ?
On Sun, 14 Dec 2003 23:59:21 +0900, David A. Black wrote:
class MyClass < MyClass
Yikes. I think that resorting to that is a reasonably good
indication that you might want to look for a new program design
–
Simon Strandgaard
I’m not sure what you mean by “duck similar” (the notion of duck
typing doesn’t scale well to “duck ”), but assuming you
mean a class that defines methods with the same names and arities as
another class, the best thing would probably be to do:
See, you knew exactly what I was talking about, and I only used two words!
Seems to scale fairly well
module A; # bunch of methods; end
module B; # same-named methods as A; endclass C; include A; end
class C; include B; end # when you want the new onesor
class C; include A; end
c = C.new
obj.extend(B)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
Yikes. I think that resorting to that is a reasonably good
indication that you might want to look for a new program design
Well, I’m working on some method wrapping implementaions, seeing how far I can
coerce Ruby into doing some of the things Peter and I are talking about. So
Yikes aren’t too out of the ordinary.
But, I actually did find a better way to do it this morning, by redefining
MyClass#new to return a subclass of itself instead. Hmm…I forget, what kind
of design pattern is that?
Thanks,
T.
On Sunday 14 December 2003 03:59 pm, David A. Black wrote:
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
class MyClass < MyClass
Yikes. I think that resorting to that is a reasonably good
indication that you might want to look for a new program designirb(main):001:0> class A < A
irb(main):002:1> def test
irb(main):003:2> super
irb(main):004:2> end
irb(main):005:1> end
TypeError: undefined superclass `A’
from (irb):1
irb(main):006:0>
I assume Tom meant that A would already exist.
Special way of writing recursive methods/classes, which reminds me of
modern c++ design’s powerful ideas of recursive classes.Is it possible to make classes which inherit from themselves ?
Yes, you just have to make sure the class exists in the first place
But I can’t imagine what good it would do. I’m not even sure in
what sense it would be “recursive”; I think all it does is duplicate
the method search path needlessly.
David
On Mon, 15 Dec 2003, Simon Strandgaard wrote:
On Sun, 14 Dec 2003 23:59:21 +0900, David A. Black wrote:
–
David A. Black
dblack@wobblini.net