Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Rob Munoz
PLUS
Rob Munoz
Courses Plus Student 586 Points

What is the easiest way to return a variable or object name from a method in Ruby?

Say I set some variable to a value as such....

some_var = 1

And I want to define a method to return the variable name rather than the value. Like so

def return_method ( argument )
    puts  "some_var"  #comment ----- if some_var = argument
end

How can I elegantly do this?

2 Answers

Polly Sileo
Polly Sileo
3,696 Points

Your function is going to return the value of a variable not the name of the variable.

If you want to return the name you could puts a string. So for your example you could run a for loop like: if some_var = argument puts "some_var"

If you didn't want to print you could also use return and return the string.