Confusion about singleton method definition

D

David Unric

Hi fellow Rubists,

I'm just learning Ruby from The Well-grounded Rubist book and I cann't
understand why I cann't get the same results as in author's example.

It's about the difference between defining singleton method directly on
an object and using class << construct.
Rephrased example follows:

MYCONST=666

myobj = Object.new

class << myobj
MYCONST=333
end

def myobj.outer_const
puts MYCONST
end

class << myobj
def inner_const
puts MYCONST
end
end


myobj.inner_const call displays 333 (singleton constant value) as
expected,
however myobj.outer_const call also displays 333 whereas it should
display the value of outer (global) MYCONST definition, ie. 666.


Did changed language definition recently somehow or is the example
and/or description in the book simply flawed ?
 
S

Su Zhang

The context is expected to differ from the top level inside
myobj.outer_const as well as inside instance methods of inner_const,
because they are binding-identical singleton methods. MYCONST in both
palces binds to the same `self', which is myobj =)

puts "Top level: #{self}"

myobj = Object.new

class << myobj
def foo
puts "class << myobj; def foo: #{self}"
end
end

def myobj.bar
puts "def myobj.bar: #{self}"
end

myobj.foo
myobj.bar
 
D

David Unric

Su Zhang> Thanks, but that confirms my results. The books preface states
it covers Ruby ver. 1.9.1 and I would wonder if it's valid no more for
Ruby 1.9.2 ?
 
D

David Unric

Anybody else can confirm there is no difference between constants scope
resolution at various kinds of singleton methods definiton?
 
R

Robert Klemme

I'm just learning Ruby from The Well-grounded Rubist book and I cann't
understand why I cann't get the same results as in author's example.

It's about the difference between defining singleton method directly on
an object and using class << construct.
Rephrased example follows:

MYCONST=3D666

myobj =3D Object.new

class << myobj
=A0MYCONST=3D333
end

def myobj.outer_const
=A0puts MYCONST
end

class << myobj
=A0def inner_const
=A0 =A0puts MYCONST
=A0end
end


myobj.inner_const call displays 333 (singleton constant value) as
expected,
however myobj.outer_const call also displays 333 whereas it should
display the value of outer (global) MYCONST definition, ie. 666.

Does it?

16:55:39 ~$ allruby r.rb
CYGWIN_NT-5.1 padrklemme2 1.7.7(0.230/5/3) 2010-08-31 09:58 i686 Cygwin
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
333
666
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
ruby 1.9.1p430 (2010-08-16 revision 28998) [i386-cygwin]
333
666
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (Java
HotSpot(TM) Client VM 1.6.0_21) [x86-java]
333
666
16:55:55 ~$ cat r.rb

MYCONST=3D666

myobj =3D Object.new

class << myobj
MYCONST=3D333
end

def myobj.outer_const
puts MYCONST
end

class << myobj
def inner_const
puts MYCONST
end
end

myobj.inner_const
myobj.outer_const
16:56:57 ~$
Did changed language definition recently somehow or is the example
and/or description in the book simply flawed ?

Can you show _exactly_ the code you executed?

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
J

Josh Cheek

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

I'm just learning Ruby from The Well-grounded Rubist book and I cann't
understand why I cann't get the same results as in author's example.

It's about the difference between defining singleton method directly on
an object and using class << construct.
Rephrased example follows:

MYCONST=666

myobj = Object.new

class << myobj
MYCONST=333
end

def myobj.outer_const
puts MYCONST
end

class << myobj
def inner_const
puts MYCONST
end
end


myobj.inner_const call displays 333 (singleton constant value) as
expected,
however myobj.outer_const call also displays 333 whereas it should
display the value of outer (global) MYCONST definition, ie. 666.

Does it?

16:55:39 ~$ allruby r.rb
CYGWIN_NT-5.1 padrklemme2 1.7.7(0.230/5/3) 2010-08-31 09:58 i686 Cygwin
========================================
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
333
666
========================================
ruby 1.9.1p430 (2010-08-16 revision 28998) [i386-cygwin]
333
666
========================================
jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (Java
HotSpot(TM) Client VM 1.6.0_21) [x86-java]
333
666
16:55:55 ~$ cat r.rb

MYCONST=666

myobj = Object.new

class << myobj
MYCONST=333
end

def myobj.outer_const
puts MYCONST
end

class << myobj
def inner_const
puts MYCONST
end
end

myobj.inner_const
myobj.outer_const
16:56:57 ~$
Did changed language definition recently somehow or is the example
and/or description in the book simply flawed ?

Can you show _exactly_ the code you executed?

Kind regards

robert
I get that result for 1.9.2



$ rvm r.rb
1.8.7(jruby-1.5.2)
333
666
1.8.7(jruby-1.5.3)
333
666
1.9.2(macruby-0.7)
333
666
1.8.7(rbx-1.1.0-20100923)
333
666
1.8.6(ruby-1.8.6-p399)
333
666
1.8.7(ruby-1.8.7-p249)
333
666
1.8.7(ruby-1.8.7-p302)
333
666
1.9.1(ruby-1.9.1-p378)
333
666
1.9.2(ruby-1.9.2-p0)
333
333


$ cat r.rb
MYCONST=666

myobj = Object.new

class << myobj
MYCONST=333
end

def myobj.outer_const
puts MYCONST
end

class << myobj
def inner_const
puts MYCONST
end
end

puts "#{RUBY_VERSION}(#{ENV['RUBY_VERSION']})"
myobj.inner_const
myobj.outer_const
 
B

botp

Anybody else can confirm there is no difference between constants scope
resolution at various kinds of singleton methods definiton?

in ruby 1.9.2, your example will provide no difference.
if you want to access the outside constant (in ruby 1.9.2), use ::MYCONST

best regards -botp
 

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,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top