[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to create singleton methods in script

charlie bowman

2/8/2006 11:58:00 PM

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


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


8 Answers

Logan Capaldo

2/9/2006 12:05:00 AM

0


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

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

>
> --
> Posted via http://www.ruby-....
>




Neowulf

2/9/2006 12:05:00 AM

0

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

charlie bowman

2/9/2006 12:17:00 AM

0

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


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


jgbailey

2/9/2006 12:22:00 AM

0

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

charlie bowman wrote:
> 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
>
>
> --
> Posted via http://www.ruby-....

Logan Capaldo

2/9/2006 12:36:00 AM

0


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.




james_b

2/9/2006 12:57:00 AM

0

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 Britt

http://www.ru... - Ruby Help & Documentation
http://www.artima.c... - The Journal By & For Rubyists
http://www.rub... - The Ruby Store for Ruby Stuff
http://www.30seco... - Building Better Tools


charlie bowman

2/9/2006 1:24:00 AM

0

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


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


Jacob Fugal

2/9/2006 4:46:00 PM

0

On 2/8/06, charlie bowman <cbowmanschool@yahoo.com> 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 Fugal