Forum: Ruby $ please expain this syntax

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
96bb15ec886c45a93e0f32bb0b2e69c8?d=identicon&s=25 Kevin Abel (bklabel1)
on 2016-09-01 17:47
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:

[code]
this_browser = $this_browser
[/code]

Please tell me what the $ sign is for on the right side of the equal
sign.

Thanks,

Kevin
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2016-09-02 00:15
A dollar sign at the start of a variable name makes it a Global
variable.
96bb15ec886c45a93e0f32bb0b2e69c8?d=identicon&s=25 Kevin Abel (bklabel1)
on 2016-09-17 00:11
Joel,
Do the globals have to be defined?
Answer is appreciated.
Thanks,
Kevin
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2016-09-17 19:26
All variables have to be defined. The interpreter defines some of them,
e.g. $0 as the initial filename.
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2016-09-19 11:22
Joel Pearson 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
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2016-09-19 16:14
> 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!
96bb15ec886c45a93e0f32bb0b2e69c8?d=identicon&s=25 Kevin Abel (bklabel1)
on 2016-09-20 19:39
Ronald Fischer wrote in post #1185038:
> Joel Pearson 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.
96bb15ec886c45a93e0f32bb0b2e69c8?d=identicon&s=25 Kevin Abel (bklabel1)
on 2016-09-20 19:42
Robert Heiler 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
This topic is locked and can not be replied to.