I took some on line classes in Ruby a few months ago. I forgot some of
the syntax. I probably need to take the classes over from the start but
I need to understand some existing code at this time. I would like to
know what the $ is for in this line of code:
this_browser = $this_browser
Please tell me what the $ sign is for on the right side of the equal
sign.
Ruby will not error out when the global variable has not been defined.
They are nil by default, always.
You can test this by the way:
In a file, say, test.rb, add:
puts $FOO
puts $FOO.class
The second line will give you back NilClass so you know that
$FOO is nil (not defined before).
You can check whether any $FOO has been set by using, for
example, defined?()
$FOO = 5
puts defined?($FOO)
Then you get a definite result.
But in general, unless you really really need a global variable
available everywhere, it may be better to use a local variable
or an @instance_variable - the latter also works on class-level
instances such as:
module Foo
@bar = 5
def self.bar?
@bar
end
end
puts Foo.bar? # outputs 5
The advantage here is that the information is stored in the
module Foo namespace and you can query it at any moment in
time via Foo.bar? which I think is pretty neat compared to
any variable like $BAR = 5. But perhaps that is due to
me not liking global variables too much - I do use them
every now and then, especially in regexes; I found that
$1 $2 matching groups are really easier on my brain; and
I also like $stdin.gets.chomp even though gets.chomp
suffices. Sometimes I can’t explain why I like something,
it just feels right for the given context!
Ruby will not error out when the global variable has not been defined.
They are nil by default, always.
You can test this by the way:
In a file, say, test.rb, add:
puts $FOO
puts $FOO.class
The second line will give you back NilClass so you know that
$FOO is nil (not defined before).
You can check whether any $FOO has been set by using, for
example, defined?()
$FOO = 5
puts defined?($FOO)
Then you get a definite result.
But in general, unless you really really need a global variable
available everywhere, it may be better to use a local variable
or an @instance_variable - the latter also works on class-level
instances such as:
module Foo
@bar = 5
def self.bar?
@bar
end
end
puts Foo.bar? # outputs 5
The advantage here is that the information is stored in the
module Foo namespace and you can query it at any moment in
time via Foo.bar? which I think is pretty neat compared to
any variable like $BAR = 5. But perhaps that is due to
me not liking global variables too much - I do use them
every now and then, especially in regexes; I found that
$1 $2 matching groups are really easier on my brain; and
I also like $stdin.gets.chomp even though gets.chomp
suffices. Sometimes I can’t explain why I like something,
it just feels right for the given context!
Appreciated.
I took some online Ruby classes a few months ago. I forgot a lot and
need to go back and watch some of them again.
Please let me know if it is OK to ask more syntax questions on this
thread. I don’t want to annoy anyone with introductory questions.
Thanks,
Kevin
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.