# Executing class methods

**URL:** <https://www.ruby-forum.com/t/executing-class-methods/134382>\
**Category:** Ruby\
**Created:** [April 10, 2008, 11:52am UTC](https://www.ruby-forum.com/t/executing-class-methods/134382 "2008-04-10T11:52:56Z")\
**Posts on this page:** 5\
**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:** [April 10, 2008, 11:52am UTC](https://www.ruby-forum.com/t/executing-class-methods/134382/1 "2008-04-10T11:52:56Z")

</div>

class MainLogic  
def archive  
sd\_a=$del\_path.zip($del\_selection)  
puts sd\_a  
sd\_a.each do |sd|  
$del\_path, $del\_selection = sd  
del = File.join $del\_path, $del\_selection  
puts “Files to be deleted: #{del}”

#Dir.chdir($del\_path)

FileUtils.rm\_r Dir.glob(del)  
end #each  
end # archive  
end #MainLogic

MainLogic.archive

Hi (again),

I wanna run the class method but somehow an error occured saying’  
testing.rb:64: undefined method `archive’ for MainLogic:Class  
(NoMethodError)’  
Is there anything wrong with my code or structure?

---

<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:** [April 10, 2008, 12:11pm UTC](https://www.ruby-forum.com/t/executing-class-methods/134382/2 "2008-04-10T12:11:28Z")

</div>

Hi,

Clement Ow wrote:

> class MainLogic  
> def archive  
> sd\_a=$del\_path.zip($del\_selection)  
> puts sd\_a  
> sd\_a.each do |sd|  
> $del\_path, $del\_selection = sd  
> del = File.join $del\_path, $del\_selection  
> puts “Files to be deleted: #{del}”
> 
> #Dir.chdir($del\_path)
> 
> FileUtils.rm\_r Dir.glob(del)  
> end #each  
> end # archive  
> end #MainLogic
> 
> MainLogic.archive
> 
> Hi (again),
> 
> I wanna run the class method but somehow an error occured saying’  
> testing.rb:64: undefined method `archive’ for MainLogic:Class  
> (NoMethodError)’  
> Is there anything wrong with my code or structure?

archive must be Singleton method.

Modify like this will work:

class MainLogic  
def MainLogic.archive  
…  
end # archive  
end #MainLogic

Or

class MainLogic  
class \<\< self  
def archive  
…  
end # archive  
end  
end #MainLogic

Regards,  
Park H.

---

<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:** [April 11, 2008, 5:42am UTC](https://www.ruby-forum.com/t/executing-class-methods/134382/3 "2008-04-11T05:42:55Z")

</div>

As defined, archive is an instance method.

try changing  
def archive

to  
def self.archive

or

def MainLogic.archive

To make it a class method.

RF

---

<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:** [April 14, 2008, 4:55am UTC](https://www.ruby-forum.com/t/executing-class-methods/134382/4 "2008-04-14T04:55:06Z")

</div>

It’s creating singleton methods. For example, you can do it on single  
objects:

```
 x = String.new

 def x.hello
   puts "Oh, hai."
 end

 x.hello
 # => Oh, hai.

```

When doing ClassName.method, you’re doing the same thing, only on a  
Class instance instead of a String instance of whatever. Now, you can  
do it with the same form as class \<\< self too. For example,

```
 class << x
   def goodbye
     puts "Oh bai."
   end
 end

 x.goodbye
 # => Oh bai.

```

In the case of class \<\< self, self is a reference to the current class  
you’re in. So, you’re adding methods to the current Class instance.

–Jeremy

On Sun, Apr 13, 2008 at 9:49 PM, Clement Ow  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> Especially for class\<\<self, is this an instance of inheritance?
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
[http://jeremymcanally.com/](http://jeremymcanally.com/)  
[http://entp.com](http://entp.com)

Read my books:  
Ruby in Practice ([Ruby in Practice](http://manning.com/mcanally/))  
My free Ruby e-book ([http://humblelittlerubybook.com/](http://humblelittlerubybook.com/))

Or, my blogs:

> **[Site not found · DreamHost](http://mrneighborly.com)**
>
> The owner of this domain has not yet uploaded their website.

[http://rubyinpractice.com](http://rubyinpractice.com)

---

<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:** [April 14, 2008, 3:49am UTC](https://www.ruby-forum.com/t/executing-class-methods/134382/5 "2008-04-14T03:49:18Z")

</div>

Ron F. wrote:

> As defined, archive is an instance method.
> 
> try changing  
> def archive
> 
> to  
> def self.archive
> 
> or
> 
> def MainLogic.archive
> 
> To make it a class method.  
> Heesob P. wrote:  
> class MainLogic  
> class \<\< self

Yeah, it works. Thanks guys! But actually out of curiosity( i think it’s  
also good to know, so that I can help anyone on this forum in future,;),  
what is it that self does?  
Especially for class\<\<self, is this an instance of inheritance?
