def method
a = 3
b = 4
some_method_that_gives # [a, b]
end
Asked
Active
Viewed 1.3k times
3 Answers
48
It outputs array of symbols, presenting variables. In your case: [:a, :b]

Michał Zalewski
- 3,123
- 2
- 24
- 37

Nakilon
- 34,866
- 14
- 107
- 142
-
Can't believe I didn't find it before. Thanks! – Cheng Dec 20 '10 at 07:06
-
1Some versions of Ruby output an array of strings instead of an array of symbols. Ruby 2.0 and 1.9 use symbols, but Ruby 1.8.7 used strings. – inquiryqueue Apr 15 '13 at 17:39
7
local_variables
lists local variables but it lists them before they are defined. See this:
p local_variables
a = 1
p local_variables
this outputs
[:a]
[:a]
which may not be what you expect. Contrast with defined?
p defined? a
a = 1
p defined? a
which outputs the more anticipated
nil
"local-variable"

starfry
- 9,273
- 7
- 66
- 96
1
if you're looking for the list with their values:
variables = self.local_variables.each_with_object({}) { |key, hash| hash[key] = eval(key.to_s) }

Khaled AbuShqear
- 1,230
- 14
- 24