Skip to content

Ruby Blender

  • Blog
  • About
  • Archives
  • Log in
 
Less
More
Trim
Untrim
« Older
Home
Loading
Newer »
Archive for August, 2009
21Aug09 Arguments with Trollop
ruby rubygems
0 Comments

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 2290

Warning: implode() [function.implode]: Argument must be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3242

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3306

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 2290

Warning: implode() [function.implode]: Argument must be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3242

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3306

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 2290

Warning: implode() [function.implode]: Argument must be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3242

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3306

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 2290

Warning: implode() [function.implode]: Argument must be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3242

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3306

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 2290

Warning: implode() [function.implode]: Argument must be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3242

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3306

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 2290

Warning: implode() [function.implode]: Argument must be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3242

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3306

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 2290

Warning: implode() [function.implode]: Argument must be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3242

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3306

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

At various points in my life, I’ve used many methods of parsing command line options, from rolling my own, to using getopt or other libraries.  Recently I came across a new one which has its own view of how to parse commandline arguments.

Trollop is, as they put it, “Yet Another Fine CommandLine Argument Parsing Library” (YAFCLAP).  It’s goal is to be as easy as possible.  You can install it as a single file in lib, or use it as a gem.  Trollop doesn’t care.  To install, you can download it from rubyforge, or use the ever popular:

  |  copy code |? 
1
gem install trollop

(you may need to prepend sudo, or run it as root).

Let’s take a look at a simple example:

  |  copy code |? 
01
require '<a class="zem_slink" title="RubyGems" rel="homepage" href="http://docs.rubygems.org">rubygems</a>'
02
require 'trollop'
03
opts = Trollop::options do
04
  opt :host, "Host to connect to", :default => "localhost"
05
  opt :port, "jmx remote port", :default => 4444
06
  opt :interval, "How often do we poll (in seconds)", :default => 10
07
  opt :count, "How many intervals, 0 == infinite, use ^C (SIGINT) to exit and write output", :default => 0
08
end
09
p opts
10

 

The first argument to opt is the name of the option.  We don’t have to specify a switch; it’s smart enough to figure out a mapping for them.  The second is a description of the argument.  You can specify a default value and/or a type.  In this example, we’ve specified defaults.  And here it is in use:

  |  copy code |? 
01
$ ruby trollop_simple.rb
02
{:interval=>10, :count=>0, :help=>false, :host=>"localhost", :port=>4444}
03
$ ruby trollop_simple.rb -h
04
Error: option '-h' needs a parameter.
05
Try --help for help.
06
$ ruby trollop_simple.rb --help
07
Options:
08
      --host, -h <s>:   Host to connect to (default: localhost)
09
      --port, -p <i>:   jmx remote port (default: 4444)
10
  --interval, -i <i>:   How often do we poll (in seconds) (default: 10)
11
     --count, -c <i>:   How many intervals, 0 == infinite, use ^C (SIGINT) to
12
                        exit and write output (default: 0)
13
          --help, -e:   Show this message
14
 
15
 
16

Notice that it has set up mappings for us as well as created a nicely formatted help — though it’s a default, without a lot of information.   Let’s add a version and usage:

  |  copy code |? 
01
require 'rubygems'
02
require 'trollop'
03
opts = Trollop::options do
04
  version <<EOF
05
jmx_mon -- we be jammin' with version zz9-za aka Arthur Dent
06
Don't forget your towel!
07
EOF
08
 
09
  banner <<EOF
10
jmx_mon is a program to monitor a jmx bean\'s value over time.
11
It can output the data as a csv, a graph in a pdf, or both.
12
 
13
Usage jmx_mon [options]
14
where [options] are:
15
EOF
16
  opt :host, "Host to connect to", :default => "localhost"
17
  opt :port, "jmx remote port", :default => 4444
18
  opt :interval, "How often do we poll (in seconds)", :default => 10
19
  opt :count, "How many intervals, 0 == infinite, use ^C (SIGINT) to exit and write output", :default => 0
20
end
21
p opts
22

Now let’s see what we have:

  |  copy code |? 
01
$ ruby trollop_simple.rb --help
02
jmx_mon is a program to monitor a jmx bean's value over time.
03
It can output the data as a csv, a graph in a pdf, or both.
04
 
05
Usage jmx_mon [options]
06
where [options] are:
07
      --host, -h <s>:   Host to connect to (default: localhost)
08
      --port, -p <i>:   jmx remote port (default: 4444)
09
  --interval, -i <i>:   How often do we poll (in seconds) (default: 10)
10
     --count, -c <i>:   How many intervals, 0 == infinite, use ^C (SIGINT) to
11
                        exit and write output (default: 0)
12
       --version, -v:   Print version and exit
13
          --help, -e:   Show this message
14
 
15
$ ruby trollop_simple.rb -v
16
jmx_mon -- we be jammin' with version zz9-za aka Arthur Dent
17
Don't forget your towel!
18
 
19

We now have a nicer usage, as well as version.  Also, it will stop automagickally if we ask for help or the version.

All of the options are placed in the opts hash; let’s see what we can do with it:

  |  copy code |? 
01
require 'rubygems'
02
require 'trollop'
03
 
04
aliases = {
05
      :MEMORY_HEAP => "Heap memory usage, multiple values",
06
      :MEMORY_HEAP_USED => "Used heap memory"
07
}
08
 
09
opts = Trollop::options do
10
  version <<EOF
11
jmx_mon -- we be jammin' with version zz9-za aka Arthur Dent
12
Don't forget your towel!
13
EOF
14
 
15
  banner <<EOF
16
jmx_mon is a program to monitor a jmx bean\'s value over time.
17
It can output the data as a csv, a graph in a pdf, or both.
18
 
19
Usage jmx_mon [options]
20
where [options] are:
21
EOF
22
  opt :host, "Host to connect to", :default => "localhost"
23
  opt :port, "jmx remote port", :default => 4444
24
  opt :interval, "How often do we poll (in seconds)", :default => 10
25
  opt :count, "How many intervals, 0 == infinite, use ^C (SIGINT) to exit and write output", :default => 0
26
  opt :aliases, "Print defined bean aliases & exit.", :default => false
27
  opt :towel, "Don't forget this!", :default => false
28
end
29
 
30
if opts[:aliases_given]
31
  aliases.each_pair do |key, value|
32
    puts "#{key}: #{value}"
33
  end
34
  exit
35
end
36
 
37
Trollop::die :towel, "You forgot your towel.  You've been eaten by a Ravenous Bugblatter Beast of Traal" unless opts[:towel]
38
 
39
p opts
40

We’ve added two more options, one for printing out aliases, and another indicating whether or not you have your towel — with deadly results should you forget.

  |  copy code |? 
1
$ ruby trollop_simple.rb -a    
2
MEMORY_HEAP: Heap memory usage, multiple values
3
MEMORY_HEAP_USED: Used heap memory
4
 
5
$ ruby trollop_simple.rb   
6
Error: argument --towel You forgot your towel.  You've been eaten by a Ravenous Bugblatter Beast of Traal.
7
Try --help for help.
8
 
9

There’s more that you can do with trollop; the website and documentation gives more examples.  However, I’d suggest considering trollop the next time you need to parse the command line.

 

 

Enhanced by Zemanta
20Aug09 Database Paranoia — ActiveRecord Callbacks
rails ruby
0 Comments

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 2290

Warning: implode() [function.implode]: Argument must be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3242

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3306

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

On a particular, nameless, heterogenous project using ruby on rails, ant, and shell scripts, there was an instance where data was not being properly cleaned before going into the database (primarily due to user errors in property files which were used by ant to feed the database).   As a result, the database was filled with extraneous spaces which were causing many issues.  A perfect illustration of GIGO.

One problem was that there were multiple databases, each with their own data.   By the time the issue was discovered, a fix was needed soonest, so the solution was less than elegant, but it does illustrate the use of callbacks in ActiveRecord:

  |  copy code |? 
01
module DbParanoia
02
# This is a really stupid thing to have to back-fit; it is making
03
# sure, however, that the users enter good data.  We ensure that
04
# strings are stripped on read and write.
05
 
06
  def trim_space
07
    self.attributes.each do |attr|
08
      attr.strip! if attr.instance_of? String
09
    end
10
  end
11
 
12
  alias_method :after_find, :trim_space
13
  alias_method :before_save, :trim_space
14
end
15

 

The above module is using the after_find and before_save callbacks to make sure that:

  1. Any spaces coming back from the database are trimmed.
  2. Any new records and/or updates will have the spaces trimmed.

You can use this method to perform pretty much any sort of validation or data manipulation of the data of an ActiveRecord class.  Other callbacks include:

  • after_create
  • after_destroy
  • after_save
  • after_update
  • after_validation
  • after_validation_on_create
  • after_validation_on_update
  • before_create
  • before_destroy
  • before_save
  • before_update
  • before_validation
  • before_validation_on_create
  • before_validation_on_update

You can read more about ActiveRecord::Callbacks at http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html.

Enhanced by Zemanta
19Aug09 Ruby Bindings and Scope
ruby
1 Comment

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 2290

Warning: implode() [function.implode]: Argument must be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3242

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3306

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 2290

Warning: implode() [function.implode]: Argument must be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3242

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3306

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 2290

Warning: implode() [function.implode]: Argument must be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3242

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3306

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 2290

Warning: implode() [function.implode]: Argument must be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3242

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3306

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

One of the features of Ruby’s Kernel module (which is imported into Object, so it’s available in every class) is the binding.  The binding allows you to keep a pointer to the scope at a particular location in your application and then later evaluate code from that location.

For example (and this came up as the result of a question on the ruby talk  mailing list), one of the things you can do is obtain a list of the local variables of a method:

  |  copy code |? 
1
def my_vars(a)
2
  b=a
3
  c=b+a
4
  puts local_variables
5
end

 

returns the three variables, a, b, and c.  That’s fine, we’re still within the scope of the method.  But how about outside of our method?  This is where #binding and the Binding class come into play:

  |  copy code |? 
1
def outside(b)
2
  puts eval("local_variables", b)
3
end
4
 
5
def my_vars(a)
6
  b=a
7
  c=b+a
8
  outside(binding)
9
end

 

One thing to note: in order to work with the binding, we have to #eval code within its scope.  Thus the call to #eval with the method we are invoking and the binding.  This leads to the question from the mailing list:

we already have #has_block? to see if a block was passed. So how about
a #has_arguments? to query if _any_ arguments have been passed — Intransition

So how would we go about writing a #has_arguments? — here is a first stab:

  |  copy code |? 
01
class Object
02
  def has_arguments?(b)
03
    vars = eval("local_variables",b)
04
    return false if vars.length == 0
05
    vars.each do |v|
06
      return false if eval("#{v}.nil?",b) 
07
    end
08
    return true
09
  end
10
end
11
 
12
class Foo
13
  def initialize(first=nil)
14
    if (has_arguments? binding)
15
      puts "We're defined"
16
    else
17
      puts "Not!"
18
    end
19
  end
20
end

The only real drawback here is that we’ve got to pass in the binding itself.  Let’s try again:

  |  copy code |? 
01
class Object
02
  def has_arguments?(&b)
03
    vars = eval("local_variables",b.binding)
04
    return false if vars.length == 0
05
    vars.each do |v|
06
      return false if eval("#{v}.nil?",b) 
07
    end
08
    return true
09
  end
10
end
11
 
12
class Foo
13
  def initialize(first=nil)
14
    if (has_arguments? {}) # note the empty block
15
      puts "We're defined"
16
    else
17
      puts "Not!"
18
    end
19
  end
20
end

In this case, instead of explicitly calling #binding, we’re passing in an empty block — the block has it’s own scope, but it happens to contain the scope of then enclosing method.  It’s still not perfect, however:

  1. You need to pass in an empty block.
  2. It fails if you have local variables other than those passed in as arguments. (Pointed out by Joel VanderWerf)

Ok, it’s got some serious flaws (Charles Nutter posted a different way of solving the problem, which does work for all cases, and doesn’t use #binding).  But it does give an idea of what you can do with #binding.

Enhanced by Zemanta
18Aug09 What’s #method_missing missing?
ruby
2 Comments

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 2290

Warning: implode() [function.implode]: Argument must be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3242

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3306

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 2290

Warning: implode() [function.implode]: Argument must be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3242

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3306

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502

#method_missing is a very useful part of the ruby language.  However, there’s one common mistake that developers make when using it.  They forget to add a call to #super.

Let’s take a look:

  |  copy code |? 
01
$ irb
02
>> class Foo
03
>>   end
04
=> nil
05
>> f=Foo.new
06
=> #<Foo:0xb75122fc>
07
>> f.i_do_not_exist
08
NoMethodError: undefined method `i_do_not_exist' for #<Foo:0xb75122fc>
09
 from (irb):5
10
>> class Foo
11
>>   def method_missing(symbol,*args)
12
>>     if (symbol.to_s === "i_do_not_exist")
13
>>       puts "Yes, actually you do"
14
>>       end
15
>>     end
16
>>   end
17
=> nil
18
>> f.i_do_not_exist
19
Yes, actually you do
20
=> nil
21
>> f.i_exist
22
=> nil

Unfortunately, we no longer get the error when we try to invoke a method which does not exist.  When we add an invocation to super, it’ll behave as expected:

  |  copy code |? 
01
>> class Foo
02
  def method_missing(symbol,*args)
03
    if (symbol.to_s === "i_do_not_exist")
04
      puts "Yes, actually you do"
05
    else   
06
      super
07
    end
08
  end
09
end
10
>>   >>     >>       >>       ?>         >>       >>     >>   => nil
11
>> f.i_exist
12
NoMethodError: undefined method `i_exist' for #<Foo:0xb75122fc>
13
 from (irb):31:in `method_missing'
14
 from (irb):35
15
>> f.i_do_not_exist
16
Yes, actually you do
17
=> nil
18
>> 

 

Enhanced by Zemanta
 
Browse Archives ยป
  • administrivia (2)
  • configuration (1)
  • metaprogramming (1)
  • mini-saga (5)
  • programming (10)
  • rails (1)
  • ruby (15)
  • rubygems (2)
  • Uncategorized (1)
 

Recent Posts

  • Default values for Attributes
  • Arguments with Trollop
  • Database Paranoia — ActiveRecord Callbacks
  • Ruby Bindings and Scope
  • What’s #method_missing missing?

Search

Browse by Category

  • administrivia (2)
  • configuration (1)
  • metaprogramming (1)
  • mini-saga (5)
  • programming (10)
  • rails (1)
  • ruby (15)
  • rubygems (2)
  • Uncategorized (1)

Browse by Tag

  • ActiveRecord
  • binding
  • Command-line interface
  • gotcha
  • irb
  • irbc
  • Languages
  • Library
  • Local variable
  • metaprogramming
  • mini-saga
  • paradigm
  • philosophy
  • programming
  • rails
  • Recursion
  • ruby
  • rubygems
  • Ruby on Rails
  • scope
  • utilities

Browse by Month

  • September 2009 (1)
  • August 2009 (4)
  • April 2009 (1)
  • February 2009 (11)
 
 
  • Blog
  • About
  • Archives
  • Log in
 


Theme Design by Jay Kwong | Powered by WordPress and K2

 

Home Top Archives Entries FeedComments Feed