From: "Jesús Gabriel y Galán" Date: 2011-03-18T17:29:04+09:00 Subject: Re: "10 Minutes to Your First Ruby Application" Deconstructed! On Fri, Mar 18, 2011 at 7:06 AM, Shangz B. wrote: > Hello and warm wishes to everyone who reads my post :) > > I am a newbie to Ruby (That Rhymes!). I am attempting to complete the > "10 Minutes to YOur First Ruby Application" tutorial and I am stuck > about the syntax of the example they are using. I get this error > message: > >  "You must pass in the path to the file to launch. > >  Usage: launcher.rb target_file" > > The above message is what the last piece of Ruby code in this > application told it to return - - I got that part, but .... > > I'm getting frustrated with the code because of the > 'placeholders'-- if that what they are -- and my confusion as if I leave > them as is or plug-in the name of the *.rb (launcher.rb) & the file_type > (rb) etc. etc. etc. (Yule Brenner ref) in place of them: > > What is app_map? Is app the keyword or is app_map the keyword or do I > stick notepad (my text editor) or ruby in its place. Same goes for > reference to 'select_app,' or 'file_type,' 'file_name,' 'app_map...' > What do I do with those? > > The attached file is the 'after' following my attempt at filling in what > I > assume  'file_name' means.  The code below is the 'b4' > > Any help would be great. I am enthusiastic about learning it, but need a > more thorough explanation of the code snippets they provide in this > tutorial. I am used to reading TSQL and MSSQL. > > Thanks in advance, you've been lovely :) > > > ############################################################ > #!/usr/local/bin/ruby > >     # Example application to demonstrate some basic Ruby features >     # This code loads a given file into an associated application > >      class Launcher >      end > >     launcher = Launcher.new > > def initialize( app_map ) >    @app_map =  app_map >  end > >   # Execute the given file using the associate app >    def run( file_name ) >        application = select_app( file_name ) >    system( "#{application} #{file_name}" ) >    end > >    # Given a file, look up the matching application >    def select_app( file_name) >        ftype = file_type( file_name) >        @app_map[ ftype ] >    end > >    # Return the part of the file name string after the last '.' >    def file_type( file_name ) >        File.extname( file_name ).gsub( /^\./, '' ).downcase >    end > > def help >  print " >  You must pass in the path to the file to launch. > >  Usage: #{__FILE__} target_file > " > end > > if ARGV.empty? >  help >  exit > else >  app_map= { >     'html' => 'firefox', >     'rb' => 'gvim', >     'jpg' => 'gimp' >  } > >  l = Launcher.new( app_map ) >  target = ARGV.join( ' ' ) >  l.run( target ) > end > > > ##################################################### > > Attachments: > http://www.ruby-forum.com/attachment/6039/RubyForum.txt I see some strange things: First of all: initialize, run, select_launcher and file_type definitions should be inside class Launcher ... end, because they should be instance methods of class Launcher. You can remove the launcher = Launcher.new, since you are not using it. After that, I think the application should work, if you call it like: ruby launcher.rb test.html it should launch firefox with test.html as a parameter. Also, the parameter name launcher in the methods run and select_launcher is confusing, since you are not passing the launcher, but the target. So, this is how it would look like (I change other minor things): #!/usr/bin/env ruby class Launcher def initialize(launcher_map) @app_map = launcher_map end # Execute the given file using the associate app def run(file) application = select_launcher(file) system ("#{application} #{file}") end # Given a file, look up the matching application def select_launcher(file) ftype = file_type(file) @app_map [ftype] end # Return the part of the file name string after the last '.' def file_type(file) File.extname(file)[1..-1].downcase end end def help puts "You must pass in the path to the file to launch." puts puts "Usage: #{__FILE__} target_file" end if ARGV.empty? help exit end app_map= {'html' => 'firefox', 'rb' => 'notepad', 'jpg' => 'gimp'} l = Launcher.new( app_map) target = ARGV.join( ' ' ) l.run( target ) Jesus.