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