# Class methods

**URL:** <https://www.ruby-forum.com/t/class-methods/142575>\
**Category:** Ruby\
**Created:** [July 9, 2008, 11:26am UTC](https://www.ruby-forum.com/t/class-methods/142575 "2008-07-09T11:26:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [July 9, 2008, 11:26am UTC](https://www.ruby-forum.com/t/class-methods/142575/1 "2008-07-09T11:26:38Z")

</div>

I have built my own ruby library where it contains my specific commands  
and methods that I need to perform certain actions and my main script.

In my ruby library I have something like:  
class Test  
def toDDMMYYYY(sep)  
strftime("%d#{sep}%m#{sep}%Y")  
end  
end

And in my main script I have something like:

require ‘PersonalRubyLib.rb’

class MainLogic  
def show\_date  
d = Date.today  
d.Test.toDDMMYYYY("-")  
end  
end

But apparently it shows an error:  
undefined method `toDDMMYYYY’ for #\<Date: 4909313  
,2299161\> (NoMethodError)

Did do wrongly in calling the class methods, maybe I am confused with  
the syntax of calling class methods? Much help is appreciated, thanks!

---

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [July 9, 2008, 1:08pm UTC](https://www.ruby-forum.com/t/class-methods/142575/2 "2008-07-09T13:08:21Z")

</div>

On Wed, Jul 9, 2008 at 11:22 AM, Clement Ow  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And in my main script I have something like:  
> But apparently it shows an error:  
> undefined method `toDDMMYYYY’ for #\<Date: 4909313  
> ,2299161\> (NoMethodError)
> 
> ## Did do wrongly in calling the class methods, maybe I am confused with the syntax of calling class methods? Much help is appreciated, thanks!
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

You did not define a class method, actually there are more ways to  
implement what you want.  
I will show you three here, it is up to you to decide what fits your  
needs best

class \<\< C = Class::new { … }  
def m; 42 end  
end

C.m

class C  
def self.m; 42 end  
end

C.m

module M  
def m; 42 end  
module\_function :m  
end

M.m

HTH  
Robert

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

AALST (n.) One who changes his name to be further to the front  
D.Adams; The Meaning of LIFF

---

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [July 9, 2008, 1:51pm UTC](https://www.ruby-forum.com/t/class-methods/142575/3 "2008-07-09T13:51:41Z")

</div>

Robert D. wrote:

> You did not define a class method

To expand, what Robert means is that you defined an instance method. To  
make it a class method in class C, you should C.m or self.m, not m  
(which would be an instance method).

Personally I prefer self.m, as I can subsequently change the name of the  
class if necessary without having to change all the class method names  
too. Laziness can be a virtue. 😉

Dave

---

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [July 10, 2008, 4:38am UTC](https://www.ruby-forum.com/t/class-methods/142575/4 "2008-07-10T04:38:59Z")

</div>

Nando S. wrote:

> Clement:
> 
> I think you have 2 different errors in your script:
> 
> 1. The error already highlighted by other forum members.
> 2. You are creating a class named “Test” and a method for it, and later  
> on you try to call this method inside an object that belongs to a  
> totally different class (Date class). If you want to add the new method  
> to the Date class you only need to write:
> 
> class Date  
> def toDDMMYYYY(sep)  
> strftime("%d#{sep}%m#{sep}%Y")  
> end  
> end
> 
> In this way the new method will be available in any date object. You  
> need to modify your main code too:
> 
> class MainLogic  
> def show\_date  
> d = Date.today  
> d.toDDMMYYYY("-")  
> end  
> end
> 
> Rgd,
> 
> Nando

Thanks for the replies, it makes Ruby learning so much easier! =)

It does work however if i change the class name to Date instead of test  
too. But what if my class is an inherited class? Maybe an example might  
explain clearer.

Assuming I have 2 classes:

class MadDate \< Date  
def toDDMMYYYY(sep = “/”)  
strftime(%d%m%Y")  
end  
end

class Main  
def self.show  
d = Date.today  
d.toDDMMYYYY("-")  
end  
end

And when i call, Main.show, it gives the same error “undefined method  
toDDMMYYYY”. Is there then a way of working around inherited classes?

---

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [July 9, 2008, 5:27pm UTC](https://www.ruby-forum.com/t/class-methods/142575/5 "2008-07-09T17:27:29Z")

</div>

Clement:

I think you have 2 different errors in your script:

1. The error already highlighted by other forum members.
2. You are creating a class named “Test” and a method for it, and later  
on you try to call this method inside an object that belongs to a  
totally different class (Date class). If you want to add the new method  
to the Date class you only need to write:

class Date  
def toDDMMYYYY(sep)  
strftime("%d#{sep}%m#{sep}%Y")  
end  
end

In this way the new method will be available in any date object. You  
need to modify your main code too:

class MainLogic  
def show\_date  
d = Date.today  
d.toDDMMYYYY("-")  
end  
end

Rgd,

Nando

---

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [July 10, 2008, 12:29pm UTC](https://www.ruby-forum.com/t/class-methods/142575/6 "2008-07-10T12:29:06Z")

</div>

> ```
> end
> 
> ```
> 
> end

hi Nando,  
since your extended class is a MadDate, you should create a  
MadDate-object, not a Date-object.

Ruud
