# Class method

**URL:** <https://www.ruby-forum.com/t/class-method/121773>\
**Category:** Ruby\
**Created:** [November 30, 2007, 7:54pm UTC](https://www.ruby-forum.com/t/class-method/121773 "2007-11-30T19:54:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pie-social](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pie-social](https://www.ruby-forum.com/u/pie-social)\
**Post date:** [November 30, 2007, 7:54pm UTC](https://www.ruby-forum.com/t/class-method/121773/1 "2007-11-30T19:54:50Z")

</div>

Need help to understand how the marked \<\<\< def works. Thanks

class Person  
def initialize(lname, fname)  
@lname = lname  
@fname = fname  
end

def lname  
return @lname  
end

def fname  
return @fname  
end

def lname=(myarg) # see = \<\<\<  
@lname = myarg  
end

def fname=(myarg)  
@fname = myarg  
end  
end

steve = Person.new(“Litt”, “Stove”)  
print "My name is ", steve.fname, " ", steve.lname, “.\n”  
steve.fname = “Steve”  
print "My name is ", steve.fname, " ", steve.lname, “.\n”

steve = Person.new(“Litt”, “Stove”)

print "My name is ", steve.fname, " ", steve.lname, “.\n”  
steve.fname = “Steve”  
print "My name is ", steve.fname, " ", steve.lname, “.\n”

#steve.fname(“Steve”) # err

#steve.lname(“Nathan”) # err ???  
steve.lname() #  
print "My name is ", steve.fname, " ", steve.lname, “.\n”

steve.lname = “Nat” #  
print "My name is ", steve.fname, " ", steve.lname, “.\n”

steve.lname() #  
print "My name is ", steve.fname, " ", steve.lname, “.\n”

I:\RubyNV\>zzz-tut13-class.rb  
My name is Stove Litt.  
My name is Steve L…  
My name is Steve L…  
My name is Steve Nat.  
My name is Steve Nat.

I:\RubyNV\>

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

if i use it stand alone it does not work!!

class Person  
def initialize(lname)  
@lname = lname  
end

def lname=(myarg) # see = \<\<\<  
@lname = myarg  
end

end

steve = Person.new(“Viswa”)

#puts steve.lname, ‘x1’ # err undefined methid lname  
#puts steve.lname(), ‘x2’ # err as above  
#puts steve.lname(“nv”), ‘x2’ # err as above

#steve.lname = “Nat” #

#puts steve.lname, ‘x2’ # err undefined methid lname  
#puts steve.lname(), ‘x3’ # # err undefined methid lname

#steve.lname = “Nat” #  
#print "My name is ", steve.lname, “.\n” #err

#steve.lname() # err  
#print "My name is ", steve.lname, “.\n”# err

---

<div class="post-metadata">

**Author:** ![pie-social](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pie-social](https://www.ruby-forum.com/u/pie-social)\
**Post date:** [November 30, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/class-method/121773/2 "2007-11-30T20:21:16Z")

</div>

On Nov 30, 2007, at 1:54 PM, Nathan V. wrote:

> end  
> @fname = myarg  
> print "My name is ", steve.fname, " ", steve.lname, “.\n”  
> steve.fname = “Steve”  
> print "My name is ", steve.fname, " ", steve.lname, “.\n”
> 
> #steve.fname(“Steve”) # err

The name of the method includes the = and the form:

steve.fname = “Steve”

is just syntactic sugar for:

steve.fname=(“Steve”)

> #steve.lname(“Nathan”) # err ???

the lname method doesn’t take any arguments. def lname

> I:\RubyNV\>zzz-tut13-class.rb  
> if i use it stand alone it does not work!!
> 
> class Person  
> def initialize(lname)  
> @lname = lname  
> end

def lname  
@lname  
end

> ## #puts steve.lname(“nv”), ‘x2’ # err as above #print "My name is ", steve.lname, “.\n”# err
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

You should get used to the shortcut to standard accessors. Rather  
than building

def myname  
@myname  
end

def myname=(arg)  
@myname = arg  
end

You can just add this to your class:

attr\_accessor :myname

In your case, the first class could be written as:

class Person  
attr\_accessor :lname, :fname

def initialize(lname, fname)  
@lname = lname  
@fname = fname  
end  
end

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![pie-social](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pie-social](https://www.ruby-forum.com/u/pie-social)\
**Post date:** [November 30, 2007, 8:24pm UTC](https://www.ruby-forum.com/t/class-method/121773/3 "2007-11-30T20:24:47Z")

</div>

On Nov 30, 2007 12:54 PM, Nathan V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> def fname  
> return @fname  
> end
> 
> def lname=(myarg) # see = \<\<\<  
> @lname = myarg  
> end

You are not defining an equals method here (=). You are defining the  
method lname=. The parser will look for this method before deciding  
it’s an assignment to variable.

> steve = Person.new(“Litt”, “Stove”)
> 
> print "My name is ", steve.fname, " ", steve.lname, “.\n”  
> steve.fname = “Steve”  
> print "My name is ", steve.fname, " ", steve.lname, “.\n”
> 
> #steve.fname(“Steve”) # err

The fname method as you have defined it does not take a parameter. It  
simply returns the fname. The correct way to call it is steve.fname  
(returns Steve)

> XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  
> end
> 
> end
> 
> steve = Person.new(“Viswa”)
> 
> #puts steve.lname, ‘x1’ # err undefined methid lname

This time you left out the lname method in the class.

> ## #steve.lname() # err #print "My name is ", steve.lname, “.\n”# err
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

You have a getter and a setter … 2 methods #lname and #lname=

Note that this is almost exactly the same thing. It builds those 2  
methods for you.

class Peep  
attr\_accessor :name  
end

p = Peep.new  
p.name = “Smith”  
puts p.name # returns Smith  
puts p.methods.grep /name/ #gives you name and name=

Todd

---

<div class="post-metadata">

**Author:** ![pie-social](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pie-social](https://www.ruby-forum.com/u/pie-social)\
**Post date:** [November 30, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/class-method/121773/4 "2007-11-30T21:06:43Z")

</div>

Nathan V. wrote:

> I:\RubyNV\>
> 
> XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
> 
> if i use it stand alone it does not work!!
> 
> class Person

> steve = Person.new(“Viswa”)
> 
> #puts steve.lname, ‘x1’ # err undefined methid lname  
> #puts steve.lname(), ‘x2’ # err as above  
> #puts steve.lname(“nv”), ‘x2’ # err as above

Here is another attempt, look at the following and notice that I had to  
define the method lname() that takes in a parameter to get the results  
that you wanted…

This is not the way to do things, just meant to show how it can be  
done… 🙂

irb(main):001:0\> class Person  
irb(main):002:1\> def lname(myarg = nil)  
irb(main):003:2\> @lname = myarg if myarg  
irb(main):004:2\> @lname  
irb(main):005:2\> end  
irb(main):006:1\> def lname=(myarg); lname(myarg); end  
irb(main):007:1\> end  
=\> nil  
irb(main):008:0\> p = Person.new  
=\> #Person:0x2ac90263d940  
irb(main):009:0\> p.lname = “jack”  
=\> “jack”  
irb(main):010:0\> p.lname(“bill”)  
=\> “bill”  
irb(main):011:0\> p.lname  
=\> “bill”

hth

ilan

---

<div class="post-metadata">

**Author:** ![pie-social](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pie-social](https://www.ruby-forum.com/u/pie-social)\
**Post date:** [November 30, 2007, 9:56pm UTC](https://www.ruby-forum.com/t/class-method/121773/5 "2007-11-30T21:56:18Z")

</div>

Hi –

On Sat, 1 Dec 2007, Todd B. wrote:

> > ```
> > return @lname
> > 
> > ```
> 
> You are not defining an equals method here (=). You are defining the  
> method lname=. The parser will look for this method before deciding  
> it’s an assignment to variable.

It’s actually the other way around (if I’m understanding your point  
correctly). If something looks like it _could_ be a local variable  
assignment, the parser will assume that it _is_ one. Therefore you  
have to use an explicit receiver for methods like lname=, if you want  
to use the syntactic sugar = thing.

David

---

<div class="post-metadata">

**Author:** ![pie-social](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pie-social](https://www.ruby-forum.com/u/pie-social)\
**Post date:** [November 30, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/class-method/121773/6 "2007-11-30T22:40:50Z")

</div>

On 30.11.2007 19:54, Nathan V. wrote:

> Need help to understand how the marked \<\<\< def works. Thanks

What does this have to do with the subject? For all I can see we are  
dealing with instance methods here.

> def fname  
> end  
> print "My name is ", steve.fname, " ", steve.lname, “.\n”  
> steve.lname() #  
> I:\RubyNV\>  
> def lname=(myarg) # see = \<\<\<  
> @lname = myarg  
> end
> 
> end
> 
> steve = Person.new(“Viswa”)
> 
> #puts steve.lname, ‘x1’ # err undefined methid lname  
> #puts steve.lname(), ‘x2’ # err as above  
> #puts steve.lname(“nv”), ‘x2’ # err as above

Well, if you do not define those methods, they are not there.

> #steve.lname = “Nat” #
> 
> #puts steve.lname, ‘x2’ # err undefined methid lname  
> #puts steve.lname(), ‘x3’ # # err undefined methid lname
> 
> #steve.lname = “Nat” #  
> #print "My name is ", steve.lname, “.\n” #err
> 
> #steve.lname() # err  
> #print "My name is ", steve.lname, “.\n”# err

For a read write attribute you need to define _both_ methods.  
Assignment and query.

def foo=(x) @foo=x end  
def foo; @foo end

Or, much simpler in a class

attr\_accessor :foo

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![pie-social](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pie-social](https://www.ruby-forum.com/u/pie-social)\
**Post date:** [November 30, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/class-method/121773/7 "2007-11-30T22:51:57Z")

</div>

On Nov 30, 2007 2:55 PM, David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > ```
> > > @fname = fname
> > > 
> > > ```
> > > 
> > > def lname=(myarg) # see = \<\<\<  
> > > have to use an explicit receiver for methods like lname=, if you want  
> > > to use the syntactic sugar = thing.
> 
> David

Yeah, after playing around a bit, I see you are right. It assumes  
assignment in the absence of an explicit receiver. I’ve never used a  
single = in a method name before (aside from the automation provided  
by the #attr\_ methods of course 🙂

Todd

---

<div class="post-metadata">

**Author:** ![pie-social](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pie-social](https://www.ruby-forum.com/u/pie-social)\
**Post date:** [December 4, 2007, 4:59am UTC](https://www.ruby-forum.com/t/class-method/121773/8 "2007-12-04T04:59:44Z")

</div>

why does line 2 state initialize instead of breed?

Here’s the modified example - p050newdog.rb

1. class NewDog
2. def initialize(breed)
3. 

```
@breed = breed

```

4. end
5. attr\_reader :breed, :name # create reader only
6. 
7. 
# setter method
8. def name=(nm)
9. 

```
@name = nm

```

10. end
11. end
12. 
13. nd = NewDog.new(‘Doberman’)
14. #nd.name=(‘Benzy’)
15. nd.name = ‘Benzy’
16. puts nd.name

---

<div class="post-metadata">

**Author:** ![pie-social](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pie-social](https://www.ruby-forum.com/u/pie-social)\
**Post date:** [December 4, 2007, 2:33am UTC](https://www.ruby-forum.com/t/class-method/121773/9 "2007-12-04T02:33:36Z")

</div>

Found this in

> **[Learn How to Blog and Build Websites for Profit!](https://heyblogging.com)**
>
> Ready to take your blogging skills to the next level? Learn how to build a website that generates income and sets you up for success.

Describes  
def methodname= usage

Programmers use the term syntactic sugar to refer to special rules that  
let you write your code in a way that doesn’t correspond to the normal  
rules but that is easier to remember how to do and looks better.

Let us say we want to set the name of a dog. As a starting point, name  
can be set along with everything else at object creation time, as in the  
example below.

1. class NewDog
2. def initialize(breed, name)
3. 

```
@breed = breed

```

4. 

```
@name = name

```

5. end
6. attr\_reader :breed, :name # create reader only
7. end
8. nd = NewDog.new(‘Doberman’, ‘Benzy’)
9. puts nd.name

class NewDog def initialize(breed, name) @breed = breed @name = name end  
attr\_reader :breed, :name # create reader only end nd =  
NewDog.new(‘Doberman’, ‘Benzy’) puts nd.name

Let’s write a set\_name method that allows us to set, or reset, the name  
of an existing dog. We’ll also rewrite the initialize method so that it  
doesn’t expect a name:

1. class NewDog
2. def initialize(breed)
3. 

```
@breed = breed

```

4. end
5. attr\_reader :breed, :name # create reader only
6. 
# setter method
7. def set\_name(nm)
8. 

```
@name = nm

```

9. end
10. end
11. nd = NewDog.new(‘Doberman’)
12. nd.set\_name(‘Benzy’)
13. puts nd.name

class NewDog def initialize(breed) @breed = breed end attr\_reader  
:breed, :name # create reader only # setter method def set\_name(nm)  
@name = nm end end nd = NewDog.new(‘Doberman’) nd.set\_name(‘Benzy’) puts  
nd.name

Ruby allows you to define methods that end with an equal sign (=). Let’s  
replace set\_name with a method called name=

1. def name=(nm)
2. @name = nm
3. end

def name=(nm) @name = nm end

name= does exactly what set\_name did, and in spite of the slightly odd  
method name, you can call it just like any other method:

1. nd.name=(‘Benzy’)

nd.name=(‘Benzy’)

Here’s the modified example - p050newdog.rb

1. class NewDog
2. def initialize(breed)
3. 

```
@breed = breed

```

4. end
5. attr\_reader :breed, :name # create reader only
6. 
7. 
# setter method
8. def name=(nm)
9. 

```
@name = nm

```

10. end
11. end
12. 
13. nd = NewDog.new(‘Doberman’)
14. #nd.name=(‘Benzy’)
15. nd.name = ‘Benzy’
16. puts nd.name

class NewDog def initialize(breed) @breed = breed end attr\_reader  
:breed, :name # create reader only # setter method def name=(nm) @name =  
nm end end nd = NewDog.new(‘Doberman’) #nd.name=(‘Benzy’) nd.name =  
‘Benzy’ puts nd.name

The equal sign gives you that familiar “assigning a value to something”  
feeling, so you know you’re dealing with a setter method. It still looks  
odd, but Ruby takes care of that, too.

Ruby gives you some syntactic sugar for calling setter methods. Instead  
of this:

1. nd.name=(‘Benzy’)

nd.name=(‘Benzy’)

you’re allowed to do this:

1. nd.name = ‘Benzy’

nd.name = ‘Benzy’

When the interpreter sees the message “name” followed by " =", it  
automatically ignores the space before equal sign and reads the single  
message “name=” - a call to the method whose name is name=, which we’ve  
defined. As for the right-hand side: parentheses are optional on single  
arguments to methods, so you can just put ‘Benzy’ there and it will be  
picked up as the argument to the name= method.

IN RAILS: Method calls using the equal-sign syntax are common in Rails  
applications.
