quick question

Discussion in 'Ruby' started by Johnathan Smith, Jan 17, 2008.

  1. Johnathan Smith, Jan 17, 2008
    #1
    1. Advertising

  2. Johnathan Smith

    Ilan Berci Guest

    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-forum.com/.
     
    Ilan Berci, Jan 17, 2008
    #2
    1. Advertising

  3. 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
     
    Justin Collins, Jan 17, 2008
    #3
  4. Johnathan Smith, Jan 17, 2008
    #4
  5. Johnathan Smith

    Ilan Berci Guest

    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-forum.com/.
     
    Ilan Berci, Jan 17, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jack
    Replies:
    1
    Views:
    904
  2. jack

    Quick perl question 2

    jack, Aug 25, 2003, in forum: Perl
    Replies:
    1
    Views:
    907
  3. Jorell

    Quick Question

    Jorell, Aug 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    313
    Jorell
    Aug 8, 2003
  4. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    663
    Iain Norman
    Feb 16, 2005
  5. JKop
    Replies:
    11
    Views:
    896
Loading...

Share This Page