what is mean by scope in ruby?

V

Vellingiri Arul

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


Thank you.
 
7

7stud --

Vellingiri said:
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
 
J

John Joyce

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.
 

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
474,001
Messages
2,570,250
Members
46,848
Latest member
Graciela Mitchell

Latest Threads

Top