# "10 Minutes to Your First Ruby Application" Deconstructed!

**URL:** <https://www.ruby-forum.com/t/10-minutes-to-your-first-ruby-application-deconstructed/204673>\
**Category:** Ruby\
**Created:** [March 18, 2011, 7:06am UTC](https://www.ruby-forum.com/t/10-minutes-to-your-first-ruby-application-deconstructed/204673 "2011-03-18T07:06:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [March 18, 2011, 7:06am UTC](https://www.ruby-forum.com/t/10-minutes-to-your-first-ruby-application-deconstructed/204673/1 "2011-03-18T07:06:31Z")

</div>

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

#####################################################

---

<div class="post-metadata">

**Author:** ![shangz\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f0a364/32.png) [@shangz\_b](https://www.ruby-forum.com/u/shangz_b)\
**Post date:** [March 18, 2011, 9:29am UTC](https://www.ruby-forum.com/t/10-minutes-to-your-first-ruby-application-deconstructed/204673/2 "2011-03-18T09:29:34Z")

</div>

On Fri, Mar 18, 2011 at 7:06 AM, Shangz B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> reference to ‘select\_app,’ or ‘file\_type,’ ‘file\_name,’ ‘app\_map…’  
> Thanks in advance, you’ve been lovely 🙂
> 
> end  
> end  
> help  
> l.run( target )  
> end
> 
> #####################################################
> 
> Attachments:  
> [http://www.ruby-forum.com/attachment/6039/RubyForum.txt](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.

---

<div class="post-metadata">

**Author:** ![shangz\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f0a364/32.png) [@shangz\_b](https://www.ruby-forum.com/u/shangz_b)\
**Post date:** [March 18, 2011, 8:35pm UTC](https://www.ruby-forum.com/t/10-minutes-to-your-first-ruby-application-deconstructed/204673/3 "2011-03-18T20:35:16Z")

</div>

Muchos Gracias Jesus I do appreciate your time. Thank you for responding  
to my post.

Thank you Jesus, ☹ unfortunately I got syntax errors and no launch of  
the application. So, wherever the word ‘file’ appears should I put in  
its place, “launcher”…? Will I have to substitute ‘rb’ wherever  
file\_type() appears? When I took out the “launcher=Launcher.new” it gave  
syntax errors as with placing the end after everything. Excuse me for  
being thick. I really would like to understand. And just so I don’t bug  
you too much is there any other place I can get an explanation of this  
particular or a breakdown of this tutorial line-by-line? I attempted the  
“Ruby Syntax” link, but I need a reference as I go through this  
tutorial.

Thanks again:)

########################################################################

“Jesús Gabriel y Galán” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in post  
#988096:

> On Fri, Mar 18, 2011 at 7:06 AM, Shangz B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > reference to ‘select\_app,’ or ‘file\_type,’ ‘file\_name,’ ‘app\_map…’  
> > Thanks in advance, you’ve been lovely 🙂
> > 
> > end  
> > end  
> > help  
> > l.run( target )  
> > end
> > 
> > #####################################################
> > 
> > Attachments:  
> > [http://www.ruby-forum.com/attachment/6039/RubyForum.txt](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.

---

<div class="post-metadata">

**Author:** ![shangz\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f0a364/32.png) [@shangz\_b](https://www.ruby-forum.com/u/shangz_b)\
**Post date:** [March 18, 2011, 9:21pm UTC](https://www.ruby-forum.com/t/10-minutes-to-your-first-ruby-application-deconstructed/204673/4 "2011-03-18T21:21:18Z")

</div>

On Mar 17, 2011, at 23:06 , 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:

It might be worth your time to check out Chris P.'s book “Learn to  
Program” just to get the basics of the syntax and semantics down first.  
[Learn to Program, by Chris Pine](http://pine.fm/LearnToProgram/) – it is a really fast read and I’ve had  
people do it in a weekend, so it is good timing. 🙂 You can go through  
the original tutorial at that url, or buy the current PDF from  
[pragprog.com](http://pragprog.com).

> "You must pass in the path to the file to launch.
> 
> Usage: launcher.rb target\_file"

You’re getting that from the following:

> exit  
> so, your ARGV.empty? evaluates to true, so you print help and exit. Pass  
> in some args on the command line and you will be on to your next problem  
> to fix, which Jesus pointed out.

One step at a time and you’ll do fine. The Chris P. book will do a  
good job of teaching you how all the different types of ruby variables  
work and how to get the syntax right. It should be a good suppliment to  
the thing you’re currently reading.

---

<div class="post-metadata">

**Author:** ![shangz\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f0a364/32.png) [@shangz\_b](https://www.ruby-forum.com/u/shangz_b)\
**Post date:** [March 19, 2011, 12:00am UTC](https://www.ruby-forum.com/t/10-minutes-to-your-first-ruby-application-deconstructed/204673/5 "2011-03-19T00:00:58Z")

</div>

[devX.com](http://devX.com) is one of the worst websites for learning anything about  
programming.

Go here:

[http://www.ruby-lang.org/en/](http://www.ruby-lang.org/en/)

and read the 20 minute ruby tutorial. Then if you are still interested  
in ruby get the Chris P. Learning to Program book.

There are lots of ruby tutorials–when you find a bad one, move on. You  
might take a look at \_why the lucky stiff’s tutorials for a “stranger”  
take on ruby.
