"10 Minutes to Your First Ruby Application" Deconstructed!

Hello and warm wishes to everyone who reads my post :slight_smile:

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 :slight_smile:

···

############################################################
#!/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

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

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.

···

On Fri, Mar 18, 2011 at 7:06 AM, Shangz B. <shangbaby@gmail.com> wrote:

Hello and warm wishes to everyone who reads my post :slight_smile:

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 :slight_smile:

############################################################
#!/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

Hello and warm wishes to everyone who reads my post :slight_smile:

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. :slight_smile: 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.

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.

Usage: #{__FILE__} target_file
"
end

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.

···

On Mar 17, 2011, at 23:06 , Shangz B. wrote:

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.

···

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

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

Thank you Jesus, :frowning: 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" <jgabrielygalan@gmail.com> wrote in post
#988096:

On Fri, Mar 18, 2011 at 7:06 AM, Shangz B. <shangbaby@gmail.com> wrote:

reference to 'select_app,' or 'file_type,' 'file_name,' 'app_map...'
Thanks in advance, you've been lovely :slight_smile:

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 = 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.

--
Posted via http://www.ruby-forum.com/\.