[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

singleton methods : when are they not permitted?

Its Me

11/30/2004 10:05:00 PM

irb(main):025:0> x = :any
=> :any
irb(main):026:0> def x.include(obj); true; end
TypeError: can't define singleton method "include" for Symbol
from (irb):26

Why no singleton methods on Symbol?

Are there other objects that are also not allowed to have singleton methods?

Thanks!


19 Answers

Florian Gross

11/30/2004 11:03:00 PM

0

itsme213 wrote:

> Why no singleton methods on Symbol?
>
> Are there other objects that are also not allowed to have singleton methods?

Singleton methods can not be defined for "immediate" Objects. Immediate
Objects are stored directly by their id which means they are very cheap
to use.

Immediate objects include all Fixnums, Symbols, true, false and nil (but
note that def true.foo; end will silently add the method directly to
TrueClass because there is guaranteed to be only one instance, true, for
it.)

You can also not add methods to Floats and Bignums (this is for
consistency with Fixnums), but this can be changed by overriding
Numeric#singleton_method_added.

It also seems not to be possible to directly add methods to literals
like this: (Not sure why -- maybe Ruby optimizes literals?)

irb(main):014:0> def ("x").foo; end
SyntaxError: compile error
(irb):14: can't define singleton method for literals

However this can be worked around trivially by using an expression instead:

irb(main):015:0> def (("x")).foo; end
=> nil

Francis Hwang

12/1/2004 12:59:00 AM

0


On Nov 30, 2004, at 6:12 PM, Florian Gross wrote:
> It also seems not to be possible to directly add methods to literals
> like this: (Not sure why -- maybe Ruby optimizes literals?)
>
> irb(main):014:0> def ("x").foo; end
> SyntaxError: compile error
> (irb):14: can't define singleton method for literals
>
> However this can be worked around trivially by using an expression
> instead:
>
> irb(main):015:0> def (("x")).foo; end
> => nil

Errrr. So what's the difference between

def ("x").foo; end

and

def (("x")).foo; end

Why do the extra parens make the parser treat it differently? My head
hurts.

Francis Hwang
http://f...



dblack

12/1/2004 1:15:00 AM

0

Christoph R.

12/1/2004 1:20:00 AM

0

itsme213 schrieb:

>irb(main):025:0> x = :any
>=> :any
>irb(main):026:0> def x.include(obj); true; end
>TypeError: can't define singleton method "include" for Symbol
> from (irb):26
>
>Why no singleton methods on Symbol?
>
>Are there other objects that are also not allowed to have singleton methods?
>
>
Yes you can't define singleton methods for Fixnums either - originally
the answers was all so called "immediate" objects (Fixnums, Symbols
nil,true, and false) which are Object existing outside of the garbage
collector but Matz implemented special singleton methods (+ singleton
class) for nil,true and false. For exmple, both

def nil.fine() end

class << true
Love = :forever
end

are okay.


Interestingly enough you can define instance variables
for all immediate values - for example

class Object
def you
p @you
end
end

:me.instance_variable_set(:@you, :you)
0.instance_variable_set(:@you, -1)

:me.you # :you
0.you # -1

/Christoph

Christoph R.

12/1/2004 1:57:00 AM

0

Francis Hwang schrieb:

>
> On Nov 30, 2004, at 6:12 PM, Florian Gross wrote:
>
>> It also seems not to be possible to directly add methods to literals
>> like this: (Not sure why -- maybe Ruby optimizes literals?)
>
Pardon my igmorance but what is a literal?

> Errrr. So what's the difference between
>
> def ("x").foo; end
>
> and
>
> def (("x")).foo; end
>
> Why do the extra parens make the parser treat it differently? My head
> hurts.

Mine too - I find these error messages rather missleading
after all both

("x") == "x"

(("x")) == "x"

evalute to true


/Christoph


dblack

12/1/2004 2:10:00 AM

0

Christoph R.

12/1/2004 2:16:00 AM

0

Florian Gross schrieb:

> itsme213 wrote:
>
>> Why no singleton methods on Symbol?
>>
>> Are there other objects that are also not allowed to have singleton
>> methods?
>
>
> Singleton methods can not be defined for "immediate" Objects.
> Immediate Objects are stored directly by their id which means they are
> very cheap to use.
>
> Immediate objects include all Fixnums, Symbols, true, false and nil
> (but note that def true.foo; end will silently add the method directly
> to TrueClass because there is guaranteed to be only one instance,
> true, for it.)
> You can also not add methods to Floats and Bignums (this is for
> consistency with Fixnums), but this can be changed by overriding
> Numeric#singleton_method_added

(Miss)Applying syllogistic logic you can add singleton methods
to Floats or Singletons you just have do deal with the exception
either by changing Numeric#singleton_method_added or simply
catching the exception

x = 2.0
class << x
def bla;end
rescue
end

x.bla

/Christoph

Jim Freeze

12/1/2004 4:19:00 AM

0

* David A. Black <dblack@wobblini.net> [2004-12-01 10:14:40 +0900]:

> a = "x"
> def (a).foo ...

My confusion is that I don't see the value in this.
I cannot access the 'just defined method foo' of "x" anyway.

Let's see..., "x".foo, oh no, wait. "x" is different than "x".
I give.

--
Jim Freeze
Code Red. Code Ruby


Robert Klemme

12/1/2004 8:40:00 AM

0


"David A. Black" <dblack@wobblini.net> schrieb im Newsbeitrag
news:Pine.LNX.4.44.0411301808310.27136-100000@wobblini...
> Hi --
>
> On Wed, 1 Dec 2004, Christoph wrote:
>
> > Francis Hwang schrieb:
> >
> > >
> > > On Nov 30, 2004, at 6:12 PM, Florian Gross wrote:
> > >
> > >> It also seems not to be possible to directly add methods to
literals
> > >> like this: (Not sure why -- maybe Ruby optimizes literals?)
> > >
> > Pardon my igmorance but what is a literal?
> >
> > > Errrr. So what's the difference between
> > >
> > > def ("x").foo; end
> > >
> > > and
> > >
> > > def (("x")).foo; end
> > >
> > > Why do the extra parens make the parser treat it differently? My
head
> > > hurts.
> >
> > Mine too - I find these error messages rather missleading
> > after all both
> >
> > ("x") == "x"
> >
> > (("x")) == "x"
> >
> > evalute to true
>
> So does:
>
> a = "x"
> a == "x"
>
> but still, during parsing a bare identifier like 'a' and a literal
> like '"x"' are not treated the same way.

You can view "x" as an object constructor (much similar to 1, [] {1=>2}
etc.). Different from 1 "x" creates new instances every time it is
executed:

09:24:22 [robert.klemme]: ruby -e '10.times { p "x".id }'
134690520
134690496
134690472
134690448
134690424
134690400
134690376
134690352
134690328
134690304

That explains why a) it doesn't make sense to do 'def "x".foo()...end'
(regardless of the number of brackets around "x") and b) Ruby issues the
warning / error message.

Kind regards

robert


Its Me

12/1/2004 2:48:00 PM

0

> You can view "x" as an object constructor (much similar to 1, [] {1=>2}
> etc.). Different from 1 "x" creates new instances every time it is
> executed:
> That explains why a) it doesn't make sense to do 'def "x".foo()...end'
> (regardless of the number of brackets around "x") and b) Ruby issues the
> warning / error message.

I'm sure I am missing something.

a = "x"
def a.foo; end
b = a
b.foo

Have I not defined a singleton method on a literal?