# Ruby classes and \<\< Syntax

**URL:** <https://www.ruby-forum.com/t/ruby-classes-and-syntax/137833>\
**Category:** Ruby\
**Created:** [May 16, 2008, 6:56pm UTC](https://www.ruby-forum.com/t/ruby-classes-and-syntax/137833 "2008-05-16T18:56:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christoph\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ccd318/32.png) [@Christoph\_S](https://www.ruby-forum.com/u/Christoph_S)\
**Post date:** [May 16, 2008, 6:56pm UTC](https://www.ruby-forum.com/t/ruby-classes-and-syntax/137833/1 "2008-05-16T18:56:39Z")

</div>

I have written a simple snippet of code and would appreciate any help  
with understanding what’s really going on.

### Creating Basic Class

class Person # Same as: class Person \< Object  
attr\_accessor :firstname  
attr\_accessor :lastname

def initialize(firstname, lastname)  
@firstname, @lastname = firstname, lastname  
end

def fullname  
@firstname + ’ ’ + @lastname  
end  
end

### Create two instances of class Person

john = Person.new(‘John’, ‘Doe’)  
john.firstname # =\> “John”  
john.lastname # =\> “Doe”  
john.to\_s # =\> “#Person:0x356678”

jack = Person.new(‘Jack’, ‘Stone’)  
jack.firstname # =\> “Jack”  
jack.lastname # =\> “Stone”  
jack.to\_s # =\> “#Person:0x3512f4”

class Person  
def to\_s  
'Person: ’ + fullname  
end  
end

john.to\_s # =\> “Person: John D.”  
jack.to\_s # =\> “Person: Jack Stone”

### Everything as expected until here…

### Now the interesing part:

class \<\<Person  
def birthday  
‘2008-05-16’  
end  
end

### NOT EXPECTED:

john.birthday # =\> NoMethodError: undefined method `birthday' for #<Person ...> jack.birthday # => NoMethodError: undefined method`birthday’ for  
#\<Person …\>

frank = Person.new(‘Frank’, ‘New’) # =\> Try with new instance  
frank.birthday # =\> NoMethodError: undefined method `birthday’ for  
#\<Person …\>

### So basically my question is: What does the ‘class \<\<Person’

Statement really do?

Thank you very much!  
Christoph S.

---

<div class="post-metadata">

**Author:** ![Christoph\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ccd318/32.png) [@Christoph\_S](https://www.ruby-forum.com/u/Christoph_S)\
**Post date:** [May 16, 2008, 7:11pm UTC](https://www.ruby-forum.com/t/ruby-classes-and-syntax/137833/2 "2008-05-16T19:11:22Z")

</div>

On Fri, May 16, 2008 at 11:54 AM, Christoph S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @firstname, @lastname = firstname, lastname  
> john.lastname # =\> “Doe”  
> end  
> end  
> end

You are defining a class method and not a class instance method. Do  
this…

class Person  
def birthday  
‘2008-05-16’  
end  
end

Todd

---

<div class="post-metadata">

**Author:** ![Christoph\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ccd318/32.png) [@Christoph\_S](https://www.ruby-forum.com/u/Christoph_S)\
**Post date:** [May 16, 2008, 7:18pm UTC](https://www.ruby-forum.com/t/ruby-classes-and-syntax/137833/3 "2008-05-16T19:18:26Z")

</div>

Which means:

class \<\<Person  
def birthday  
‘2008-05-16’  
end  
end

Is EXACTLY the same as:

class Person  
def self.birthday  
‘2008-05-16’  
end  
end

Or:

class Person  
class \<\<self  
def birthday  
‘2008-05-16’  
end  
end  
end

Thank you very much!  
Christoph S.

---

<div class="post-metadata">

**Author:** ![Christoph\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ccd318/32.png) [@Christoph\_S](https://www.ruby-forum.com/u/Christoph_S)\
**Post date:** [May 17, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/ruby-classes-and-syntax/137833/4 "2008-05-17T16:07:20Z")

</div>

Which means:

class \<\<Person  
def birthday  
‘2008-05-16’  
end  
end

Is EXACTLY the same as:

class Person  
def self.birthday  
‘2008-05-16’  
end  
end

Or:

class Person  
class \<\<self  
def birthday  
‘2008-05-16’  
end  
end  
end

True, but all 3 are class methods and none will work as you intend.

You would have to call Person.birthday to get a response and everyone  
will have the same birthday (what a party!)  
Of course you knew that, but some readers might not and I am bored, so  
I wrote this.
