I’d like to access a global variable from within a function, without
passing it as paramater. How do I do it? e.g
def func
puts i
end
i = 5
func
In PHP, for example, I could use the “global” keyword in the function.
I’d like to access a global variable from within a function, without
passing it as paramater. How do I do it? e.g
def func
puts i
end
i = 5
func
In PHP, for example, I could use the “global” keyword in the function.
Vitaly Belman wrote:
I’d like to access a global variable from within a function, without
passing it as paramater. How do I do it? e.g
You want to use a global variable, like so:
def func
puts $i
end
$i = 5
func
The “$” tells Ruby it’s global.
In PHP, for example, I could use the “global” keyword in the function.
Yeah… one of the most distracting “features” of PHP shrugs.
Sebastian
Vitaly Belman wrote:
I’d like to access a global variable from within a function, without
passing it as paramater. How do I do it?
Global variables start with a “$” so just write
def func
puts $i
end
$i = 5
func
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs