Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,728 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,728 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