$ please expain this syntax

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.

Thanks,

Kevin

A dollar sign at the start of a variable name makes it a Global
variable.

Joel,
Do the globals have to be defined?
Answer is appreciated.
Thanks,
Kevin

Joel P. wrote in post #1185032:

All variables have to be defined.

This is not correct. Global variables (those with $) are the exception.
They can be used without having ever been set.

However, if you enable warnings, you will get a

 warning: global variable ... not initialized

All variables have to be defined. The interpreter defines some of them,
e.g. $0 as the initial filename.

Do the globals have to be defined?

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!

Ronald F. wrote in post #1185038:

Joel P. wrote in post #1185032:

All variables have to be defined.

This is not correct. Global variables (those with $) are the exception.
They can be used without having ever been set.

However, if you enable warnings, you will get a

 warning: global variable ... not initialized

Appreciated.

Robert H. wrote in post #1185040:

Do the globals have to be defined?

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