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 Ruby Modules Include and Extend Being Included

Deepak Rohan Sekar
Deepak Rohan Sekar
9,634 Points

Different ways to write a method

In ruby, what is the difference between the two below method definitions?

def get_name(name)
     @name = name
end

def get_name=(name)
     @name = name
end

5 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hmm. I apologize.

I've just came back to Ruby and really don't remember this syntax. Apparently it is a "Setter Method" or "Virtual Attribute." I'm sorry I can't explain it better as I need to research this a bit more myself. But, here are a few links that may be of help:

  1. Stack Overflow

  2. Treehouse Post from Grace Kelly.

  3. Ruby Cookbook from Safari books online.

Hope these help. :dizzy:

Andrei Burichita
Andrei Burichita
9,989 Points

"Method names in Ruby may contain upper-case and lower-case letters, numbers, underscores _ and the punctation signs !, ?, =.

A method name can't begin with a number, and the characters !, ? and = can only appear at the end.

Non-ASCII characters can be used in a method name, but this can lead to very confusing situations and should not be common practice. "

http://stackoverflow.com/questions/10542354/what-are-the-restrictions-for-method-names-in-ruby

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Deepak,

As far as I know, the second one is incorrect syntax and will result in an error from the compiler. Arguments are passed into a method with parenthesis only with no equal sign.

Where did you see this syntax? I've never seen that before.

:dizzy:

Andrei Burichita
Andrei Burichita
9,989 Points

the only difference is that small "=" which is accepted when defining a function. in this case the first function name is get_name and the second function name is get_name= That is all, no errors should be present ..

wait = (attribute) should only be used when you're writing a writer or assigner method that assigns the value of some instance method. or maybe it works with class methods too? not sure about that. but yeah. you just wrote a setter and getter methods. so it shouldn't be get_name=(name), it should be I think name=(name), also get_name(the getter) should not take in any arguments.

Andrei Burichita
Andrei Burichita
9,989 Points

yes but in this case the name of a function can take the "=" sign or the "?" this is allowed in ruby as it is only a name... compile the code and see that there are no error messages