简体   繁体   中英

Can we print the value of a local variable by using its corresponding symbol in Ruby?

This is just demo code:

I have say below local variables. Now the local_variables method would list them.

name = "john"
sex = "M"
age = 20
dob = "12/12/1998"

p local_variables #[:name, :sex, :age, :dob]

Can we use those symbols to print the values they have been assigned?

As an example:

p name #"john"

But I am trying to check if I get the same value "john" using :name or not.

使用eval

eval("name") # "john"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM