# Scope

**URL:** <https://www.ruby-forum.com/t/scope/188033>\
**Category:** Ruby\
**Created:** [May 4, 2010, 3:55pm UTC](https://www.ruby-forum.com/t/scope/188033 "2010-05-04T15:55:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eduardo\_Mucelli\_RSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/d9b06d/32.png) [@Eduardo\_Mucelli\_RSS](https://www.ruby-forum.com/u/Eduardo_Mucelli_RSS)\
**Post date:** [May 4, 2010, 3:55pm UTC](https://www.ruby-forum.com/t/scope/188033/1 "2010-05-04T15:55:40Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Eduardo\_Mucelli\_RSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/d9b06d/32.png) [@Eduardo\_Mucelli\_RSS](https://www.ruby-forum.com/u/Eduardo_Mucelli_RSS)\
**Post date:** [May 4, 2010, 4:51pm UTC](https://www.ruby-forum.com/t/scope/188033/2 "2010-05-04T16:51:18Z")

</div>

2010/5/4 Eduardo Mucelli R. Oliveira [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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  
“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?

> 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

---

<div class="post-metadata">

**Author:** ![Eduardo\_Mucelli\_RSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/d9b06d/32.png) [@Eduardo\_Mucelli\_RSS](https://www.ruby-forum.com/u/Eduardo_Mucelli_RSS)\
**Post date:** [May 4, 2010, 6:50pm UTC](https://www.ruby-forum.com/t/scope/188033/3 "2010-05-04T18:50:09Z")

</div>

On Tue, May 4, 2010 at 9:55 AM, Eduardo Mucelli R. Oliveira  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

> # 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)

---

<div class="post-metadata">

**Author:** ![Eduardo\_Mucelli\_RSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/d9b06d/32.png) [@Eduardo\_Mucelli\_RSS](https://www.ruby-forum.com/u/Eduardo_Mucelli_RSS)\
**Post date:** [May 4, 2010, 9:12pm UTC](https://www.ruby-forum.com/t/scope/188033/4 "2010-05-04T21:12:24Z")

</div>

On 4 maio, 13:49, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end
> > 
> > # The two classes described above are here
> 
> or
> 
> > method `to\_b’ for string." What is the scope point in here,  
> > 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)

Thanks, that solved my scope doubt.
