Scope

  • Thread starter Eduardo Mucelli R. Oliveira
  • Start date
E

Eduardo Mucelli R. Oliveira

I have a script with those two following classes:

class String
def to_b
["true"].include?(self.downcase)
end
end

class HereIWantUseToB
"string".to_b
end

Ok, that works fine, the to_b method can be "seen" by
HereIWantToUseToB class. But I wanted to those two classes inside
another class, or module, like this:

class Everything
# The two classes described above are here
end

module Everything
# The two classes described above are here
end

Anyway, for both cases I have the message "NoMethodError: undefined
method `to_b’ for string." What is the scope point in here, what is
the best way solve that so ?
 
R

Robert Klemme

2010/5/4 Eduardo Mucelli R. Oliveira said:
I have a script with those two following classes:

class String
=A0 def to_b
=A0 =A0 =A0["true"].include?(self.downcase)
=A0 end
end

Why are you doing it so complicated? Why not just this:

def to_b
"true" =3D=3D downcase
end
class HereIWantUseToB
=A0 "string".to_b
end

You are using #to_b on a string constant that is not attached anywhere
(e.g. as class or instance variable). What exactly are you trying to
do?
Ok, =A0that works fine, the to_b method can be "seen" by
HereIWantToUseToB class. But I wanted to those two classes inside
another class, or module, like this:

class Everything
=A0 # The two classes described above are here
end

module Everything
=A0 # The two classes described above are here
end

Anyway, for both cases I have the message "NoMethodError: undefined
method `to_b=92 for string." What is the scope point in here, what is
the best way solve that so ?

Please show the complete code that leads to the error.

Kind regards

robert

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

Rick DeNatale

I have a script with those two following classes:

class String
=A0 def to_b
=A0 =A0 =A0["true"].include?(self.downcase)
=A0 end
end

This opens up the system class String (or ::String if you prefer to
explicitly specify the outer scope) and adds an instance method
available to any instance of ::String
class HereIWantUseToB
=A0 "string".to_b
end

Ok, =A0that works fine, the to_b method can be "seen" by
HereIWantToUseToB class. But I wanted to those two classes inside
another class, or module, like this:

class Everything
=A0 # The two classes described above are here
end

module Everything
=A0 # The two classes described above are here
end


Either

class Everything
class String
#...
end
end

or

module Everything
class String
#...
end
end

creates a new class Everything::String, which is unrelated to ::String

Anyway, for both cases I have the message "NoMethodError: undefined
method `to_b=92 for string." What is the scope point in here,

A string literal, like "string', or "foo" will ALWAYS be an instance
of ::String, and never an instance of Everything::String



--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
E

Eduardo Mucelli R. Oliveira

I have a script with those two following classes:
class String
  def to_b
     ["true"].include?(self.downcase)
  end
end

This opens up the system class String (or ::String if you prefer to
explicitly specify the outer scope) and adds an instance method
available to any instance of ::String




class HereIWantUseToB
  "string".to_b
end
Ok,  that works fine, the to_b method can be "seen" by
HereIWantToUseToB class. But I wanted to those two classes inside
another class, or module, like this:
class Everything
  # The two classes described above are here
end
module Everything
  # The two classes described above are here
end

Either

class Everything
  class String
     #...
  end
end

or

module Everything
  class String
     #...
  end
end

creates a new class Everything::String, which is unrelated to ::String
Anyway, for both cases I have the message "NoMethodError: undefined
method `to_b’ for string." What is the scope point in here,

A string literal, like "string', or "foo" will ALWAYS be an instance
of ::String, and never an instance of Everything::String

--
Rick DeNatale

Blog:http://talklikeaduck.denhaven2.com/
Github:http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR:http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn:http://www.linkedin.com/in/rickdenatale

Thanks, that solved my scope doubt.
 

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

Similar Threads

Booleans 6
scope when reopening method 3
Binding scope 10
extend quesion 8
scope question 7
Scope Missunderstanding 3
Scope resolution operator 4
Code sharing 2

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top