[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

quick question

Johnathan Smith

1/17/2008 9:12:00 PM

hey

how do i access i global variable in ruby?

thanks
--
Posted via http://www.ruby-....

4 Answers

Ilan Berci

1/17/2008 9:14:00 PM

0

Johnathan Smith wrote:
> hey
>
> how do i access i global variable in ruby?
>
> thanks

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

ilan


--
Posted via http://www.ruby-....

Justin Collins

1/17/2008 9:19:00 PM

0

Ilan Berci wrote:
> Johnathan Smith wrote:
>
>> hey
>>
>> how do i access i global variable in ruby?
>>
>> thanks
>>
>
> 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


Johnathan Smith

1/17/2008 9:20:00 PM

0

cheers guys
--
Posted via http://www.ruby-....

Ilan Berci

1/17/2008 9:40:00 PM

0

Justin Collins wrote:
> Ilan Berci wrote:
>>
>> 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

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>
--
Posted via http://www.ruby-....