Executing class methods

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?

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.

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

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
[email protected] wrote:

Especially for class<<self, is this an instance of inheritance?


Posted via http://www.ruby-forum.com/.


http://jeremymcanally.com/
http://entp.com

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

Or, my blogs:
http://mrneighborly.com
http://rubyinpractice.com

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?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs