This tutorial assumes that you’ve already downloaded and installed the gem as instructed here.

In the following tutorial, [t0fuu] will be my prompt at the command line.

Starting off

Let’s try starting off with a simple console application – one that can fit into a single file. When you installed the simpleconsole gem, you got access to a new binary script – simcon.

[t0fuu] simcon
Usage: simcon [generator] [--file output_file] [--options]

Available generators and their usage:
  application [--file output_file] [-c controller_name | -v view_name]
  controller [--file output_file] [-c controller_name]
  rakefile
  single [--file output_file] [-c controller_name | -v view_name]
  view [--file output_file] [-v view_name]

The generators will create a new file given by the '-f output_file' argument.  
If no '--file' was given, it will output to STDOUT.  Try 'simcon help [generator]' 
for more info about each generator.

Available options:
  -f | --file FILENAME
  -c | --controller CONTROLLER
  -v | --view VIEW

This is a code generator, just to get you started. For now, just ignore everything and use the single generator.

[t0fuu] simcon single -f myapp
The file 'myapp' was generated.
[t0fuu] chmod a+x myapp

Now open up the file ‘myapp’ in your editor, and you should see this:

#!/usr/bin/env ruby -w
require "rubygems" 
require "simpleconsole" 
# require File.dirname(__FILE__) + "/../" 

class Controller < SimpleConsole::Controller
end

class View < SimpleConsole::View
end

SimpleConsole::Application.run(ARGV, Controller, View)

That’s the entire outline of your application. You can separate the Controller and View class in different files (and use generators to generate those files) but since this is a short example, we’ll stick to one file.

Pay attention to the first line, `#!/usr/bin/env ruby -w`. This works on most *nix systems but if you use a different path to ruby, you may want to change that.

Let’s try out our application so far.

[t0fuu] ruby myapp
/opt/local/lib/ruby/gems/1.8/gems/simpleconsole-0.1.0/lib/application.rb:45:
in `run': undefined method `default' for #<Controller:0x606694           
@params={:action=>:default, :id=>nil}> (NoMethodError)
        from myapp:12

There’s an error for an undefined method, `default`. That’s because by default, when your application is called with no arguments, SimpleConsole will try to run the method `default` defined in your controller.

Let’s fix that, inside your Controller class, write this:

class Controller < SimpleConsole::Controller
  def default
    puts "Hello World!" 
  end
end

Now try running it:

[t0fuu] ruby myapp
Hello World!

Neat. But if we don’t want to litter our Controller with calls to `puts`. Let’s separate the logic from the view. This time, get rid of your `puts` call and put this into your Controller and View”

class Controller < SimpleConsole::Controller
  def default
    @message = "Hello World!" 
  end
end

class View < SimpleConsole::View
  def default
    puts @message
  end
end

If you run myapp now, you should get the same results. Any instance variable that you set in your controller, you will have access to in your view.

So that’s our default action. What if we wanted to make another action? Well, simpleconsole follows this pattern:

[t0fuu] myapp action [--option param]

Where action is just a method in your controller or view. Lets define a new action, `whoami`. We want it to do something like this -

[t0fuu] myapp whoami --name Hugh
Hello, your name is Hugh

To do this, we need to define a new few things:

class Controller < SimpleConsole::Controller
  params :string => {:n => :name}

  def whoami
    @name = params[:name]
  end

  # ...
end

class View < SimpleConsole::View
  def whoami
    puts "Hello, your name is " + @name
  end
end

And that’s it! In the Controller class, we’re making a call to the class method `params`. This sets up the shorthand and longhand arguments that your application can take.

[t0fuu] myapp whoami --name Hugh
Hello, your name is Hugh

Parameter Parsing

The parameter parsing (the call to the class method `params`) might be a bit confusing. Here’s some more info on that.

Using the example:

params :string => {:n => :name}

This just means we’re accepting a string in the form of `myapp -n string` or `myapp—name string`. So whatever argument is after `-n` or `--name` is passed into the hash `params[:name]` for you to use in the Controller.

There are also a few other types you can use: Here’s an example of parameter parsing -
class Controller < SimpleConsole::Controller
  params :string => {:n => :name},
         :text => {:d => :description},
         :bool => {:o => :old},
         :int => {:a = :age}

  # ...
end
Now from the command line, we can do this:
[t0fuu] ruby myapp --name Sensei -d "old and wise" --old -a 98

Inside our Controller, we’ll have access to all of these arguments through the params hash.

params[:name]           # => "Sensei" 
params[:description]    # => "old and wise" 
params[:old]            # => true
params[:age]            # => 98

More?

Check out the documentation. Let me know if you have any bug issues, questions, or you just want to talk about Ruby. My email is bienhd@gmail.com.