Class#name=

Is there any reason that Class#name= shouldn’t exist?

It would be useful for giving a descriptive name to anonymous classes.

Josh

Hi,

In message “Re: Class#name=”
on Fri, 23 Jun 2006 06:42:09 +0900, Joshua H.
[email protected] writes:

|Is there any reason that Class#name= shouldn’t exist?

Modules and classes are named by the constant referencing it. If you
want to name an anonymous module, see the following code:

mod = Module.new
NewModule = mod
p NewModule.name # => “NewModule”

						matz.

Hi,

In message “Re: Class#name=”
on Fri, 23 Jun 2006 18:12:27 +0900, “Robert D.”
[email protected] writes:

|irb(main):003:0> A = mod
|=> A
|irb(main):004:0> mod.name
|=> “A”
|most surprising
|irb(main):005:0> B = mod
|=> A
|irb(main):006:0> mod.name
|=> “A”
|even more surprising
|irb(main):007:0> B.name
|=> “A”
|???
|irb(main):008:0>
|
|Well this kind of shocks me, is there an idea behind this which I am
|missing?

The module referenced from A and B are the same object (mod). If same
module reports different name depends on how it is called, I would
surprise VERY MUCH. For your note, Ruby does not promise that you
don’t surprise. It’s just impossible. It tries its best to do
reasonable job as much as it can.

						matz.

On 6/23/06, Yukihiro M. [email protected] wrote:

want to name an anonymous module, see the following code:

mod = Module.new
NewModule = mod
p NewModule.name # => “NewModule”

                                                    matz.

I have been inspired by this, and I found this behavior
irb
irb(main):001:0> mod = Module.new
=> #Module:0xb7d8d024
irb(main):002:0> mod.name
=> “”
ok understandable
irb(main):003:0> A = mod
=> A
irb(main):004:0> mod.name
=> “A”
most surprising
irb(main):005:0> B = mod
=> A
irb(main):006:0> mod.name
=> “A”
even more surprising
irb(main):007:0> B.name
=> “A”
???
irb(main):008:0>

Well this kind of shocks me, is there an idea behind this which I am
missing?
If so could you please point me there.
Sorry for being so direct, but that seems to be in violation with “Ruby
should do what you expect it to do”.

Cheers
Robert


Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein

Hi –

On Fri, 23 Jun 2006, Robert D. wrote:

Modules and classes are named by the constant referencing it. If you
irb(main):001:0> mod = Module.new
=> A
If so could you please point me there.
Sorry for being so direct, but that seems to be in violation with “Ruby
should do what you expect it to do”.

As in most such cases, it does, instead, something that makes more
sense, in light of Ruby’s design, than the thing one might have
expected :slight_smile:

The design is such that, given this:

a = obj
b = a

a and b are references to the same object. So:

a.meth

and

b.meth

are exactly the same thing. It’s all about sending messages to
objects, and in this case, you’re sending the same message to the same
object, and (predictably) it responds the same way both times.

David


David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

See what the readers are saying about “Ruby for Rails”!

In message “Re: Class#name=”
on Fri, 23 Jun 2006 20:06:31 +0900, “Robert D.”
[email protected] writes:

|We have an object x, ok we create it now :slight_smile:
| x= mod.new
|oh it has a name method, let us see
| x.name => “”
|great, that is a good name.
|BTW I need a constant reference to that object
| A = x
|A.name will be “” of course I just assigned another reference to the object,
|I am a clever guy, I know that pointing another reference to an object can
|not possibly change the
|behavior (please let us be precise here, behavior not sate, state is
|internal, behavior is
|what counts to the programmer)
| A.name == x.name => true
|As I told you, I am sooo great
| A.name => “A”
|What??? You must be kidding no way.

Ruby looks for the name through class hierarchy, when it’s asked a
name for the class/module that has not been named yet.

						matz.

On 6/23/06, Yukihiro M. [email protected] wrote:

|=> “A”
|

I really did appreciate your reply, I really really did not want to be
too
direct or not polite but obviously I did not make myself clear. :frowning:

First let us establish what we all three seem to agree to, ty David too.

  • Two references to the same object will respond identical to an
    identical
    message. Agreed?
  • It is impossible to gurantee expected behavior, I understand that rule
    as
    “Ruby will not do something weird unless there is a good reason”.
  • I made one very stupid ??? above because I had already commented on
    the
    behavior of the same object.

What follows will not ever doubt these principles.

If you are still with me (and if you are not please silently and politly
ignore that post, I will survive ) the key question is, and please take
no
offence I will use a wording appropriate to how I feel about it.

We have an object x, ok we create it now :slight_smile:
x= mod.new
oh it has a name method, let us see
x.name => “”
great, that is a good name.
BTW I need a constant reference to that object
A = x
A.name will be “” of course I just assigned another reference to the
object,
I am a clever guy, I know that pointing another reference to an object
can
not possibly change the
behavior (please let us be precise here, behavior not sate, state is
internal, behavior is
what counts to the programmer)
A.name == x.name => true
As I told you, I am sooo great
A.name => “A”
What??? You must be kidding no way.

ok, ok you convinced me I see the reason now, but I am a quick learner
let
us go on,
“A” is even better a name than “”, much longer, indefinitely longer,
well
seems to be a very, very long name :wink:

B = x

Of course
B.name == x.name => true
Ha you cannot kill a good man, and of course
B.name == “B” => false
B.name => A
What did you do to me???
I am dead, unless, please, please you explain or change ruby :wink:

Hopefully I was not too blunt.

Cheers
Robert


Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein

On 6/23/06, Yukihiro M. [email protected] wrote:

Ruby looks for the name through class hierarchy, when it’s asked a

name for the class/module that has not been named yet.

                                                    matz.

Strange, when looking at it like this it starts making sense to me :slight_smile:
Thank you for going through the trouble answering this.

Cheers
Robert

  • Albert Einstein

Hi –

On Fri, 23 Jun 2006, Robert D. wrote:

If you are still with me (and if you are not please silently and politly
A.name will be “” of course I just assigned another reference to the object,
I am a clever guy, I know that pointing another reference to an object can
not possibly change the
behavior (please let us be precise here, behavior not sate, state is
internal, behavior is
what counts to the programmer)
A.name == x.name => true

As I told you, I am sooo great
A.name => “A”
What??? You must be kidding no way.

But look at x.name:

irb(main):001:0> x = Module.new
=> #Module:0x316ddc
irb(main):002:0> A = x
=> A
irb(main):003:0> x.name
=> “A”

The assigment of the constant means that this module is henceforth
identified by that constant; that’s its name.

So the next question is… what happens when there’s another constant
assignment to the same module? The module can’t have multiple names,
so the answer is that it retains its first name.

It’s like doing:

S = String
S.name # => the name of this class is still “String”

except you’re starting with an anonymous module, giving it a name (A =
x), and then assigning it to a new constant.

David


David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

See what the readers are saying about “Ruby for Rails”!

Yukihiro M. wrote:

|irb(main):005:0> B = mod
|missing?

The module referenced from A and B are the same object (mod). If same
module reports different name depends on how it is called, I would
surprise VERY MUCH. For your note, Ruby does not promise that you
don’t surprise. It’s just impossible. It tries its best to do
reasonable job as much as it can.

Hmm… This occured to me:

cut Bc < B
def name; ‘B’; end
end

B.name #=> B
A.name #=> A

I’m not saying this is how it works or should work neccessarily, it
just makes me realize that it might possible b/c the cut is a separate
object.

OTOH, one wonders if maybe module#name isn’t such a good thing.
Considering my own use of the method, I think in every case I have been
dissatisfied with the solution and eventually removed it.

T.

I guess it’s because constants are constant so they can not be
re-assigned

Robert D. schrieb:

Hi –

On Fri, 23 Jun 2006, Pete wrote:

I guess it’s because constants are constant so they can not be re-assigned

$ ruby -e ‘A=1; A=2; puts A’
-e:1: warning: already initialized constant A
2

:slight_smile:

The original example didn’t involve constant re-assignment, though,
just assignment of two constants to the same object.

David


David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

See what the readers are saying about “Ruby for Rails”!

In message “Re: Class#name=”
on Fri, 23 Jun 2006 23:27:38 +0900, Christian N.
[email protected] writes:

|Probably not worth trying to fix that “bug”.

Especially since constant redefinition is not common.

						matz.

Yukihiro M. [email protected] writes:

|Well this kind of shocks me, is there an idea behind this which I am
|missing?

The module referenced from A and B are the same object (mod). If same
module reports different name depends on how it is called, I would
surprise VERY MUCH. For your note, Ruby does not promise that you
don’t surprise. It’s just impossible. It tries its best to do
reasonable job as much as it can.

That’s in generally a good idea, but it should be noted that:

irb(main):001:0> A = Class.new
irb(main):002:0> B = A
irb(main):003:0> A.name
=> “A”
irb(main):004:0> B.name
=> “A”
irb(main):005:0> A = nil
irb(main):006:0> B.name
=> “A”
irb(main):007:0> A.name
NoMethodError: undefined method `name’ for nil:NilClass
from (irb):7
from :0

Probably not worth trying to fix that “bug”.