# What is mean by scope in ruby?

**URL:** <https://www.ruby-forum.com/t/what-is-mean-by-scope-in-ruby/117347>\
**Category:** Ruby\
**Created:** [October 16, 2007, 6:02am UTC](https://www.ruby-forum.com/t/what-is-mean-by-scope-in-ruby/117347 "2007-10-16T06:02:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vellingiri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@vellingiri](https://www.ruby-forum.com/u/vellingiri)\
**Post date:** [October 16, 2007, 6:02am UTC](https://www.ruby-forum.com/t/what-is-mean-by-scope-in-ruby/117347/1 "2007-10-16T06:02:56Z")

</div>

Hai friends,  
Please anybody can answer this question.  
What is mean by scope in ruby.

Thank you.

---

<div class="post-metadata">

**Author:** ![vellingiri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@vellingiri](https://www.ruby-forum.com/u/vellingiri)\
**Post date:** [October 16, 2007, 6:52am UTC](https://www.ruby-forum.com/t/what-is-mean-by-scope-in-ruby/117347/2 "2007-10-16T06:52:26Z")

</div>

Vellingiri A. wrote:

> Hai friends,  
> Please anybody can answer this question.  
> What is mean by scope in ruby.

A scope is the portion of your code in which a variable can be given a  
value and then retrieved.

num = 10

puts num + 1 #11

def func  
puts num + 2  
end

puts num + 3 #13

func #Error

The variable num is defined within a scope that includes all the areas  
outside of the method definition func. However, inside the func method,  
num is not “in scope” because the method definition creates a new scope:

def func  
num = 10  
puts num + 5  
end

func #15  
puts num + 6 #error

---

<div class="post-metadata">

**Author:** ![vellingiri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@vellingiri](https://www.ruby-forum.com/u/vellingiri)\
**Post date:** [October 16, 2007, 8:18am UTC](https://www.ruby-forum.com/t/what-is-mean-by-scope-in-ruby/117347/3 "2007-10-16T08:18:45Z")

</div>

scope is a common idea in programming languages.  
If a variable is “in scope” it can be accessed from that part of a  
program.  
If it is “out of scope” it is invisible in that part of the program.

Everything in your neighbor’s house is out of scope to you!  
Everything in your house is out of scope to your neighbor!  
Everything in the street in front of your house might be in scope for  
you and your neighbor.
