how to create singleton methods in script

C

charlie bowman

Here is a simplified version of a little timekeeper app that I've been
using to learn ruby with. The TimeKeeper class is a class I created
also. I want to add a method to the script that displays some text if a
bad action occurs but I can't get it to work. I don't know how to tell
the script to use the method ( or function as I would call it in perl).
This is my error.

undefined method `display_bad_action' for #<TimeKeeper:0xb7fc8730>
(NoMethodError

usr/bin/ruby

require 'TimeKeeper'

## create the TimeKeeper object
time_keeper = TimeKeeper.new(ARGV)
puts time_keeper.display_bad_action(time_keeper.action)

## method to display messages regarding bad action (ie can't logout when
on break)
def time_keeper.display_bad_action(action)
case action
when 'break'
return 'You are currently clocked in. Your options are: out or
break'
else
return @status
return "I don\'t recognize your last action (#{@status}) recorded
action. Please fix it in the log."
end
end
 
N

Neowulf

You may want to try putting your method def's above the code that calls
the methods.

This is something I fell into when I first started as well.

~Neowulf
 
L

Logan Capaldo

Here is a simplified version of a little timekeeper app that I've been
using to learn ruby with. The TimeKeeper class is a class I created
also. I want to add a method to the script that displays some text
if a
bad action occurs but I can't get it to work. I don't know how to
tell
the script to use the method ( or function as I would call it in
perl).
This is my error.

undefined method `display_bad_action' for #<TimeKeeper:0xb7fc8730>
(NoMethodError

usr/bin/ruby

require 'TimeKeeper'

## create the TimeKeeper object
time_keeper = TimeKeeper.new(ARGV) # Ok
puts time_keeper.display_bad_action(time_keeper.action) # Oops,
this method doesn't exist, YET

## method to display messages regarding bad action (ie can't logout
when
on break)
def time_keeper.display_bad_action(action) # NOW, I know how to
call this method.
[code omitted]
end
try time_keeper = TimeKeeper.new(ARGV)
then def time_keeper.display_bad_action then foollow that with
any code that might follow it.
 
C

charlie bowman

The following code works but it's awfully ugly putting a method at the
top of a script. Is this really how I have to structure a script?

#!/usr/bin/ruby

require 'TimeKeeper'

public
## method to display messages regarding bad action (ie can't logout
when on break)
def display_bad_action(action)
case action
when 'break'
return 'You are currently clocked in. Your options are: out or
break'
else
return 5
end
end

## create the TimeKeeper object
time_keeper = TimeKeeper.new(ARGV)
puts display_bad_action(time_keeper.action)
 
M

m4dc4p

Just move the method definition to beforer you call it (i.e. above puts
time_keeper.display_bad_action)
 
L

Logan Capaldo

The following code works but it's awfully ugly putting a method at the
top of a script. Is this really how I have to structure a script?

Well yes and no. If you stick display_bad_action in the TimeKeeper
class it doesn't matter whether you call it from another method
before it "sees" the definition. The rule is, before you call a
method (at runtime chronologically, not necessarily before you type
the code to call the method) it must be defined.

so

class A
def a
b
end
def b
puts "B called"
end
end

a = A.new
a.a

is fine.
 
J

James Britt

charlie said:
The following code works but it's awfully ugly putting a method at the
top of a script. Is this really how I have to structure a script?

There seems to be a conflict of aesthetics.

I tend to find it awfully ugly seeing methods strewn about an
application, without being part of a class with a well-defined purpose.

But, granted, the extra typing needed to define and instantiate a class
may be overkill for a really small utility script. Then you'll just
have to accept how the Ruby parser handles source files. Which, under
other circumstances, affords you all sorts of neat behavior.

Basically, Ruby is not Perl.

--
James Britt

http://www.ruby-doc.org - Ruby Help & Documentation
http://www.artima.com/rubycs/ - The Journal By & For Rubyists
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.30secondrule.com - Building Better Tools
 
C

charlie bowman

James said:
There seems to be a conflict of aesthetics.

I tend to find it awfully ugly seeing methods strewn about an
application, without being part of a class with a well-defined purpose.
I Generally agree with you but there are certain circumstances where you
would like to add a "specific to your app" method to a general class. I
like to put these at the bottom of a script (which is what I do in
perl). If you were going to add a single "specific" method to a
"general" class would you create a whole new class for this one method
our would you put it at the top of your script?
 
J

Jacob Fugal

I Generally agree with you but there are certain circumstances where you
would like to add a "specific to your app" method to a general class. I
like to put these at the bottom of a script (which is what I do in
perl). If you were going to add a single "specific" method to a
"general" class would you create a whole new class for this one method
our would you put it at the top of your script?

Third option: I'd put it into an extension module, than have the
instance extend that module:

galadriel:~$ cat > foo.rb
class Foo
def bar
puts "bar"
end
end

galadriel:~$ cat > baz.rb
module Baz
def baz
puts "baz"
end
end

galadriel:~$ cat > test.rb
require 'foo'
require 'baz'

foo =3D Foo.new
foo.extend Baz
foo.bar
foo.baz

galadriel:~$ ruby test.rb
bar
baz

Jacob Fugal
 

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

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top