Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Accessing global variable from a function

Reply
Thread Tools

Accessing global variable from a function

 
 
Vitaly Belman
Guest
Posts: n/a
 
      01-28-2006
I'd like to access a global variable from within a function, without
passing it as paramater. How do I do it? e.g

def func
puts i
end

i = 5
func

In PHP, for example, I could use the "global" keyword in the function.

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


 
Reply With Quote
 
 
 
 
Sebastian Steinlechner
Guest
Posts: n/a
 
      01-28-2006
--------------enig211ED926D3528FDDFC47BC1B
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

Vitaly Belman wrote:
> I'd like to access a global variable from within a function, without
> passing it as paramater. How do I do it? e.g


You want to use a global variable, like so:

def func
puts $i
end

$i = 5
func

The "$" tells Ruby it's global.

> In PHP, for example, I could use the "global" keyword in the function.


Yeah... one of the most distracting "features" of PHP *shrugs*.


Sebastian

--
It was mentioned on CNN that the new prime number discovered recently is
four times bigger than the previous record.
~ John Blasik

--------------enig211ED926D3528FDDFC47BC1B
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFD2+iK8X9UC2T3lakRApbyAJ9FzNIQ4g0wHiGeHwG7Dn LCtGL9yQCfUsGq
zTgoouW0ar8zHOt13LxUspU=
=Vl1C
-----END PGP SIGNATURE-----

--------------enig211ED926D3528FDDFC47BC1B--


 
Reply With Quote
 
 
 
 
JustAGuest
Guest
Posts: n/a
 
      01-28-2006
Vitaly Belman wrote:
> I'd like to access a global variable from within a function, without
> passing it as paramater. How do I do it?

Global variables start with a "$" so just write

def func
puts $i
end

$i = 5
func







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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing variable from a function within a function Nathan Harmston Python 6 06-24-2007 09:14 PM
Can we make a local variable in a function as global variable??? sairam Python 2 04-05-2007 07:16 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
Accessing a global variable when there is a local variable in the same name Mohanasundaram C Programming 44 08-23-2004 11:17 PM



Advertisments