# How to create singleton methods in script

**URL:** <https://www.ruby-forum.com/t/how-to-create-singleton-methods-in-script/53545>\
**Category:** Ruby\
**Created:** [February 9, 2006, 12:57am UTC](https://www.ruby-forum.com/t/how-to-create-singleton-methods-in-script/53545 "2006-02-09T00:57:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [February 9, 2006, 12:57am UTC](https://www.ruby-forum.com/t/how-to-create-singleton-methods-in-script/53545/1 "2006-02-09T00:57:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [February 9, 2006, 1:05am UTC](https://www.ruby-forum.com/t/how-to-create-singleton-methods-in-script/53545/2 "2006-02-09T01:05:38Z")

</div>

On Feb 8, 2006, at 6:58 PM, charlie bowman wrote:

> undefined method `display\_bad\_action’ for #TimeKeeper:0xb7fc8730
> 
> ## 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.

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [February 9, 2006, 1:08am UTC](https://www.ruby-forum.com/t/how-to-create-singleton-methods-in-script/53545/3 "2006-02-09T01:08:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [February 9, 2006, 1:23am UTC](https://www.ruby-forum.com/t/how-to-create-singleton-methods-in-script/53545/4 "2006-02-09T01:23:31Z")

</div>

Just move the method definition to beforer you call it (i.e. above puts  
time\_keeper.display\_bad\_action)

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [February 9, 2006, 1:16am UTC](https://www.ruby-forum.com/t/how-to-create-singleton-methods-in-script/53545/5 "2006-02-09T01:16:19Z")

</div>

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)

Neowulf wrote:

> 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

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [February 9, 2006, 1:56am UTC](https://www.ruby-forum.com/t/how-to-create-singleton-methods-in-script/53545/6 "2006-02-09T01:56:56Z")

</div>

charlie bowman wrote:

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

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

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [February 9, 2006, 2:23am UTC](https://www.ruby-forum.com/t/how-to-create-singleton-methods-in-script/53545/7 "2006-02-09T02:23:35Z")

</div>

James B. wrote:

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

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [February 9, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/how-to-create-singleton-methods-in-script/53545/8 "2006-02-09T17:46:23Z")

</div>

On 2/8/06, charlie bowman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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 = Foo.new
foo.extend Baz
foo.bar
foo.baz

```

galadriel:~$ ruby test.rb  
bar  
baz

Jacob F.

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [February 9, 2006, 1:36am UTC](https://www.ruby-forum.com/t/how-to-create-singleton-methods-in-script/53545/9 "2006-02-09T01:36:28Z")

</div>

On Feb 8, 2006, at 7:16 PM, charlie bowman wrote:

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