Forum: Ruby how to create singleton methods in script

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 Charlie Bowman (beachguy82)
on 2006-02-09 00:57
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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-02-09 01:05
(Received via mailing list)
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.
D8376641b56fed67d99599d5e7bf9631?d=identicon&s=25 Neowulf (Guest)
on 2006-02-09 01:08
(Received via mailing list)
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
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 Charlie Bowman (beachguy82)
on 2006-02-09 01:16
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
573b9499030e1ccb867ef80f0ff1ac49?d=identicon&s=25 m4dc4p (Guest)
on 2006-02-09 01:23
(Received via mailing list)
Just move the method definition to beforer you call it (i.e. above puts
time_keeper.display_bad_action)
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-02-09 01:36
(Received via mailing list)
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.
Bc6d88907ce09158581fbb9b469a35a3?d=identicon&s=25 James Britt (Guest)
on 2006-02-09 01:56
(Received via mailing list)
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.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
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 Charlie Bowman (beachguy82)
on 2006-02-09 02:23
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?
Cff9eed5d8099e4c2d34eae663aae87e?d=identicon&s=25 Jacob Fugal (Guest)
on 2006-02-09 17:46
(Received via mailing list)
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
This topic is locked and can not be replied to.