local variables - Ruby local_variables returns :symbols? -


i looking through ruby kernel doc , saw method:

a = 2 local_variables # => [:a, :_] 

why return :a , not a? thought ":" reserved symbols, symbol :a doesn't point variable nor it's assigned value, 2.

furthermore, how go accessing actual variables through method? in b=local_variables.first (would 2, :a).

is there reason behind behavior, it?

thanks/

why return :a , not a? thought ":" reserved symbols

it's expected behavior. according docs:

returns names of current local variables.

so yes, returns array of symbols.


furthermore, how go accessing actual variables through method?

as noted jonathan camenisch, ruby 2.1 introduced binding#local_variable_get:

a = 2 binding.local_variable_get(:a) #=> 2 

for older rubies, use eval:

a = 2 eval(:a.to_s) #=> 2 

is there reason behind behavior, it?

in ruby symbols used references:

"foo".methods #=> [:<=>, :==, :===, :eql?, :hash, :casecmp, ...]  module.constants #=> [:object, :module, :class, :basicobject, :kernel, :nilclass, ...] 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -