Executing class methods

C

Clement Ow

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?
 
H

Heesob Park

Hi,

Clement said:
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 Heesob
 
R

Ron Fox

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
 
C

Clement Ow

Ron said:
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 said:
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?
 
J

Jeremy McAnally

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

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?



--
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top