1.respond_to? :define_singleton_method # => true

T

timr

In 1.9,NoMethodError: undefined method `define_singletone_method' for
1:Fixnum
from (irb):7
from /usr/local/bin/irb19:12:in `<main>'

It therefore seems that 1.respond_to? :define_singleton_method should
return false as it did in 1.8.7.=> false

Does anyone understand why Fixnum objects list this method but denies
knowing of it, when they are sent the message?
Thanks,
Tim
 
S

serialhex

[Note: parts of this message were removed to make it a legal post.]

umm...
NoMethodError: undefined method `define_singletone_method' for
1:Fixnum
from (irb):7
from /usr/local/bin/irb19:12:in `<main>'

the first thing i do when i get an error is spell-check :p

anyway, when i run it i get:

irb(main):001:0> 1.respond_to? :define_singleton_method
=> true
irb(main):002:0> 1.methods.include?:)define_singleton_method)
=> true
irb(main):003:0> 1.define_singleton_method:)new_method){}
TypeError: can't define singleton
from (irb):3:in `define_singleton_method'
from (irb):3
from C:/Ruby192/bin/irb:12:in `<main>'
irb(main):004:0>

it's prolly to protect the coolness of integers & stuff, as from what i
understand all the basic integers are pre-defined & immutable.

hex
 
G

Gary Wright

=20
it's prolly to protect the coolness of integers & stuff, as from what = i
understand all the basic integers are pre-defined & immutable.

Fixnums are not 'immutable' in that you can associate state with a =
Fixnum instance and change that state:

class Fixnum
attr_accessor :note
end

1.note =3D "the loneliest number"
1.note =3D "the second natural number"

The essential properties of a fixnum, the 'oneness' of 1, the 'twoness' =
of 2, are immutable in that those properties are associated with the =
unique identity of each fixnum instance and not with any state =
associated with the instance.

It is my understanding that the prohibition on singleton methods is =
because there doesn't seem to be an efficient way to implement them for =
objects that are implicitly represented by their object id (fixnum, =
symbol, nil, true, false).

Gary Wright=
 
T

timr

Hi Gary,
But you can add singleton methods to nil and true, so the problem with
adding singleton methods to Fixnum objects is not related to the fact
that they have a predetermined object_id.
I am nil
=> nilSyntaxError: compile error
(irb):5: syntax error, unexpected tINTEGER
def 1.new_method
^
(irb):7: syntax error, unexpected kEND, expecting $end
from (irb):7
from :0I am so true
=> nil
 
T

timr

hex,
Yes embarrassing typing/spell check failure. Sorry. But regardless,
you see the issue--singleton methods cannot be defined on numbers.
Gary suggested that other objects with permanent object_ids like nil,
true, false etc. are likely the same also. But they actually are not,
you can define singleton methods for them. So I am still curious why
the difference in behavior of these Fixnum objects. It seems arbitrary
to disallow singleton methods on numbers--regardless of how
infrequently you would want to attach a singleton method to a number.
Tim


[Note:  parts of this message were removed to make it a legal post.]

umm...
NoMethodError: undefined method `define_singletone_method' for
1:Fixnum
       from (irb):7
       from /usr/local/bin/irb19:12:in `<main>'

the first thing i do when i get an error is spell-check :p

anyway, when i run it i get:

 irb(main):001:0> 1.respond_to? :define_singleton_method
=> true
irb(main):002:0> 1.methods.include?:)define_singleton_method)
=> true
irb(main):003:0> 1.define_singleton_method:)new_method){}
TypeError: can't define singleton
        from (irb):3:in `define_singleton_method'
        from (irb):3
        from C:/Ruby192/bin/irb:12:in `<main>'
irb(main):004:0>

it's prolly to protect the coolness of integers & stuff, as from what i
understand all the basic integers are pre-defined & immutable.

hex
 
T

timr

It looks like there are two classes, Fixnum and Symbol, that produce
objects which cannot have singleton methods. This is the only
practical difference between these classes and regularly referenced
classes.

From Matz' Orielly book:

3.8.1.1 Immediate values
We've said that all values in Ruby are objects and all objects are
manipulated by reference. In the reference implementation, however,
Fixnum and Symbol objects are actually "immediate values" rather than
references. Neither of these classes have mutator methods, so Fixnum
and Symbol objects are immutable, which means there is really no way
to tell that they are manipulated by value rather than by reference.

The existence of immediate values should be considered an
implementation detail. The only practical difference between immediate
values and reference values is that immediate values cannot have
singleton methods defined on them. (Singleton methods are explained in
6.1.4)

Tim
 
G

Gary Wright

Hi Gary,
But you can add singleton methods to nil and true, so the problem with
adding singleton methods to Fixnum objects is not related to the fact
that they have a predetermined object_id.

Well since there is only one instance of NilClass, TrueClass, and =
FalseClass (nil, true, false) you can implement 'singleton' methods by =
simply adding a plain old instance method to the class. So in these =
particular cases there is an efficient way to implement the singleton =
methods.

In fact:

ruby-1.9.2-p0 > nil.singleton_class
=3D> NilClass=20
ruby-1.9.2-p0 > true.singleton_class
=3D> TrueClass=20
ruby-1.9.2-p0 > false.singleton_class
=3D> FalseClass=20

Gary Wright
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top