"10 Minutes to Your First Ruby Application" Deconstructed!

S

Shangz B.

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
 
J

Jesús Gabriel y Galán

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:

=A0"You must pass in the path to the file to launch.

=A0Usage: 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 =A0'file_name' means. =A0The 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

=A0 =A0 # Example application to demonstrate some basic Ruby features
=A0 =A0 # This code loads a given file into an associated application

=A0 =A0 =A0class Launcher
=A0 =A0 =A0end

=A0 =A0 launcher =3D Launcher.new

def initialize( app_map )
=A0 =A0@app_map =3D =A0app_map
=A0end

=A0 # Execute the given file using the associate app
=A0 =A0def run( file_name )
=A0 =A0 =A0 =A0application =3D select_app( file_name )
=A0 =A0system( "#{application} #{file_name}" )
=A0 =A0end

=A0 =A0# Given a file, look up the matching application
=A0 =A0def select_app( file_name)
=A0 =A0 =A0 =A0ftype =3D file_type( file_name)
=A0 =A0 =A0 =A0@app_map[ ftype ]
=A0 =A0end

=A0 =A0# Return the part of the file name string after the last '.'
=A0 =A0def file_type( file_name )
=A0 =A0 =A0 =A0File.extname( file_name ).gsub( /^\./, '' ).downcase
=A0 =A0end

def help
=A0print "
=A0You must pass in the path to the file to launch.

=A0Usage: #{__FILE__} target_file
"
end

if ARGV.empty?
=A0help
=A0exit
else
=A0app_map=3D {
=A0 =A0 'html' =3D> 'firefox',
=A0 =A0 'rb' =3D> 'gvim',
=A0 =A0 'jpg' =3D> 'gimp'
=A0}

=A0l =3D Launcher.new( app_map )
=A0target =3D ARGV.join( ' ' )
=A0l.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 =3D 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 =3D launcher_map
end

# Execute the given file using the associate app
def run(file)
application =3D select_launcher(file)
system ("#{application} #{file}")
end

# Given a file, look up the matching application
def select_launcher(file)
ftype =3D 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=3D {'html' =3D> 'firefox', 'rb' =3D> 'notepad', 'jpg' =3D> 'gimp'}
l =3D Launcher.new( app_map)
target =3D ARGV.join( ' ' )
l.run( target )

Jesus.
 
S

Shangz B.

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=3DLauncher.new" it gav=
e
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:)



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

t
#988096:
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

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 =3D Launcher.new, since you are not using i= t.
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 =3D launcher_map
end

# Execute the given file using the associate app
def run(file)
application =3D select_launcher(file)
system ("#{application} #{file}")
end

# Given a file, look up the matching application
def select_launcher(file)
ftype =3D 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=3D {'html' =3D> 'firefox', 'rb' =3D> 'notepad', 'jpg' =3D> 'gim= p'}
l =3D Launcher.new( app_map)
target =3D ARGV.join( ' ' )
l.run( target )

Jesus.

-- =

Posted via http://www.ruby-forum.com/.=
 
R

Ryan Davis

Hello and warm wishes to everyone who reads my post :)
=20
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 Pine's book "Learn to =
Program" just to get the basics of the syntax and semantics down first. =
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.
"You must pass in the path to the file to launch.
=20
Usage: launcher.rb target_file"

You're getting that from the following:
def help
print "
You must pass in the path to the file to launch.
=20
Usage: #{__FILE__} target_file
"
end
=20
if ARGV.empty?
help
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 Pine 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.
 
7

7stud --

devX.com is one of the worst websites for learning anything about
programming.

Go here:

http://www.ruby-lang.org/en/

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top