# Scope

**URL:** <https://rubytalk.org/t/scope/58394>\
**Category:** ruby-talk\
**Created:** [4 May 2010 13:55 UTC](https://rubytalk.org/t/scope/58394 "2010-05-04T13:55:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eduardo\_Mucelli\_R\_Ol](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@Eduardo\_Mucelli\_R\_Ol](https://rubytalk.org/u/Eduardo_Mucelli_R_Ol)\
**Post date:** [4 May 2010 13:55 UTC](https://rubytalk.org/t/scope/58394/1 "2010-05-04T13:55:07Z")

</div>

I have a script with those two following classes:

class String  
&nbsp;&nbsp;&nbsp;def to\_b  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;["true"].include?(self.downcase)  
&nbsp;&nbsp;&nbsp;end  
end

class HereIWantUseToB  
&nbsp;&nbsp;&nbsp;"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  
&nbsp;&nbsp;&nbsp;# The two classes described above are here  
end

module Everything  
&nbsp;&nbsp;&nbsp;# 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 ?

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [4 May 2010 14:50 UTC](https://rubytalk.org/t/scope/58394/2 "2010-05-04T14:50:56Z")

</div>

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

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

def to\_b  
&nbsp;&nbsp;"true" == downcase  
end

> class HereIWantUseToB  
> "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, 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 ?

Please show the complete code that leads to the error.

Kind regards

robert

> **···**
>
> 2010/5/4 Eduardo Mucelli R. Oliveira \<edumucelli@gmail.com\>:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [4 May 2010 16:49 UTC](https://rubytalk.org/t/scope/58394/3 "2010-05-04T16:49:32Z")

</div>

> 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  
&nbsp;&nbsp;class String  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#...  
&nbsp;&nbsp;end  
end

or

module Everything  
&nbsp;&nbsp;class String  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#...  
&nbsp;&nbsp;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

> **···**
>
> On Tue, May 4, 2010 at 9:55 AM, Eduardo Mucelli R. Oliveira \<edumucelli@gmail.com\> wrote:
> 
> --  
> Rick DeNatale
> 
> Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
> Twitter: @RickDeNatale  
> WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![Eduardo\_Mucelli\_R\_Ol](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@Eduardo\_Mucelli\_R\_Ol](https://rubytalk.org/u/Eduardo_Mucelli_R_Ol)\
**Post date:** [4 May 2010 19:11 UTC](https://rubytalk.org/t/scope/58394/4 "2010-05-04T19:11:26Z")

</div>

Thanks, that solved my scope doubt.

> **···**
>
> On 4 maio, 13:49, Rick DeNatale \<rick.denat...@gmail.com\> wrote:
> 
> > On Tue, May 4, 2010 at 9:55 AM, Eduardo Mucelli R. Oliveira \> \> \<edumuce...@gmail.com\> wrote:  
> > \> 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/](http://talklikeaduck.denhaven2.com/)  
> > Github:[rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
> > Twitter: @RickDeNatale  
> > WWR:[http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> > LinkedIn:[http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
