quick question

J

Justin Collins

Ilan said:
Trick question! There are no globals in ruby! What do I win?

ilan

Sorry...

Global variables begin with $

irb(main):001:0> def a_method
irb(main):002:1> puts $global_var
irb(main):003:1> end
=> nil
irb(main):004:0> a_method
nil
=> nil
irb(main):005:0> $global_var = "Hi, there. I'm a global"
=> "Hi, there. I'm a global"
irb(main):006:0> a_method
Hi, there. I'm a global
=> nil

-Justin
 
I

Ilan Berci

Justin said:
Sorry...

Global variables begin with $

irb(main):001:0> def a_method
irb(main):002:1> puts $global_var
irb(main):003:1> end
=> nil
irb(main):004:0> a_method
nil
=> nil
irb(main):005:0> $global_var = "Hi, there. I'm a global"
=> "Hi, there. I'm a global"
irb(main):006:0> a_method
Hi, there. I'm a global
=> nil

-Justin

Sorry.. I had a brain fart.. was thinking of methods.. should have read
more carefully..

irb(main):001:0> $global_var = "global!"
=> "global!"
irb(main):002:0> global_variables.grep /global_var/
=> ["$global_var"]
irb(main):003:0>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top