[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Kernel.local_variables question

Jos Backus

12/16/2004 4:14:00 AM

Consider

def foo
a = "a"
b = 1
p Kernel.local_variables
end

Why doesn't this print anything? I would expect it to print

["a", "b"]

but instead it prints nothing.

--
Jos Backus _/ _/_/_/ Sunnyvale, CA
_/ _/ _/
_/ _/_/_/
_/ _/ _/ _/
jos at catnook.com _/_/ _/_/_/ require 'std/disclaimer'


4 Answers

Shashank Date

12/16/2004 4:22:00 AM

0

Jos Backus wrote:

> Consider
>
> def foo
> a = "a"
> b = 1
> p Kernel.local_variables
> end
>
> Why doesn't this print anything? I would expect it to print
>
> ["a", "b"]
>
> but instead it prints nothing.
>

Which version of ruby and which platform?
It works for me:
------------------------------------------
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>irb -v
irb 0.9(02/07/03)

C:\>irb
irb(main):001:0> def foo; a = "a"; b = 1; p Kernel.local_variables; end
=> nil
irb(main):002:0> foo
["a", "b"]
=> nil
irb(main):003:0> exit

C:\>ruby -v
ruby 1.8.2 (2004-06-29) [i386-mswin32]

C:\>

Jos Backus

12/16/2004 4:43:00 AM

0

On Thu, Dec 16, 2004 at 01:22:13PM +0900, Shashank Date wrote:
> It works for me:
> ------------------------------------------
> Microsoft Windows XP [Version 5.1.2600]
> (C) Copyright 1985-2001 Microsoft Corp.
>
> C:\>irb -v
> irb 0.9(02/07/03)
>
> C:\>irb
> irb(main):001:0> def foo; a = "a"; b = 1; p Kernel.local_variables; end
> => nil
> irb(main):002:0> foo
> ["a", "b"]
> => nil
> irb(main):003:0> exit
>
> C:\>ruby -v
> ruby 1.8.2 (2004-06-29) [i386-mswin32]
>
> C:\>

This is with the current ruby from the FreeBSD ports system:

ruby 1.8.2 (2004-07-29) [i386-freebsd6]

It doesn't work with today's HEAD either (just tried it), which is

ruby 1.9.0 (2004-12-16) [i386-freebsd6.0]

I figured with this working it would be possible to fake Python's `vars()',
useful for quick conversions of Python code.

--
Jos Backus _/ _/_/_/ Sunnyvale, CA
_/ _/ _/
_/ _/_/_/
_/ _/ _/ _/
jos at catnook.com _/_/ _/_/_/ require 'std/disclaimer'


nobu.nokada

12/16/2004 5:15:00 AM

0

Hi,

At Thu, 16 Dec 2004 13:43:15 +0900,
Jos Backus wrote in [ruby-talk:123764]:
> This is with the current ruby from the FreeBSD ports system:
>
> ruby 1.8.2 (2004-07-29) [i386-freebsd6]
>
> It doesn't work with today's HEAD either (just tried it), which is
>
> ruby 1.9.0 (2004-12-16) [i386-freebsd6.0]

It works for me.

$ ./ruby -v -e '
def foo
a = "a"
b = 1
p Kernel.local_variables
end
foo
'
ruby 1.9.0 (2004-12-16) [i686-linux]
["a", "b"]

--
Nobu Nakada


Jos Backus

12/16/2004 5:23:00 AM

0

Nevermind, it would help if I called foo, which I wasn't. Duh.

My apologies, especially to Shashank and Nobu, for the noise. Been busy
porting a Python script to Ruby all day. Bad excuse, I know.

--
Jos Backus _/ _/_/_/ Sunnyvale, CA
_/ _/ _/
_/ _/_/_/
_/ _/ _/ _/
jos at catnook.com _/_/ _/_/_/ require 'std/disclaimer'