What is mean by scope in ruby?

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

Thank you.

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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs