Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 "10 Minutes to Writing Your First Ruby Application"
Index -> Programming, Ruby -> Ruby Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
shangbaby




PostPosted: Sat Mar 19, 2011 1:51 pm   Post subject: "10 Minutes to Writing Your First Ruby Application"

I found this tutorial at devx.com
Very Happy I would appreciate a line-by-line explanation of how to:

1) make this code work as expected on my machine
2) what am I supposed to plug-in to get my file's application to execute
3) Am I supposed to plug-in the file wherever 'file_name' appears in the code.
4) What does app_map mean? Is '_map' a function of Ruby?
5) I will appreciate any help given. Thank You for your time.

Quote:

#!/usr/local/bin/ruby << my path for this is C:\ruby\bin\ruby.exe

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

launcher = Launcher.new <<<< where does this go, if anywhere?
class Launcher

def initialize( app_map ) <<<<<do I plug-in my text editor or the name of the file, or the extension of the text editor +'_map'?
@app_map = app_map
end

# Execute the given file using the associate app
def run( file_name ) <<<<<<again, place the full file name with extension or just the file name or leave as is?
application = select_app( file_name ) <<<<<<<<<<<what to plug-in for 'select_app'; is it just select_txt or select_notepad or notepad? Same with 'application'; what do I put there or leave as-is?
system( "#{application} #{file_name}" )
end

# Given a file, look up the matching application
def select_app( file_name )
ftype = file_type( file_name )<<<<<<what to plug-in for file_type
@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

end
[/b]
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sat Mar 19, 2011 5:30 pm   Post subject: RE:"10 Minutes to Writing Your First Ruby Application"

Have you done any programming before? It doesn't appear so.

The first line is *nix-only. That tells your computer which interpreter to run the file with. Since Windows uses file extensions to determine what to open the file with, you don't need it.

'launcher = Launcher.new' creates an object of type Launcher. You can see that class Launcher is defined just below it.

I'm not quite sure what app_map is; it looks like an array, but what it represents I'm not sure. This looks to be either missing code or using a separate library that hasn't been included.

Anyway, it looks like this script will launch applications, rather than creating them as your topic implies.

I suggest looking at a more basic Ruby tutorial before diving in to something complicated.
neufelni




PostPosted: Sat Mar 19, 2011 6:44 pm   Post subject: Re: "10 Minutes to Writing Your First Ruby Application"

The code defines a Launcher class that allows you to open different file types with the appropriate application. Here's an example of how it would be used:
Quote:

class Launcher

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

end

launcher = Launcher.new({'txt' => 'notepad', 'rb' => 'ruby'})
launcher.run('test.txt')



The first method in the class, called initialize, is the class constructor. This gets called when we call Launcher.new, and creates a new Launcher object. You need to pass in a Hash, which it saves to an instance variable(@ defines an instance variable) called app_map. {'txt' => 'notepad', 'rb' => 'ruby'} defines the map we are providing the classes, mapping file extensions to an appropriate application name. You can then call the run method on the newly created launcher object, passing in a file you want to open. The run method calls some of the other methods in the class to extract the file extension from the name we pass in, and then look that extension up in the app_map to find the application corresponding to that extension. It then uses the Ruby system function to execute a command such as 'notepad test.txt', opening the file in the application.


Insectoid is right, you should probably start with a basic Ruby tutorial. Try going through the Ruby Quick Start here:
http://www.ruby-lang.org/en/documentation/quickstart/
tomasdore




PostPosted: Wed Apr 27, 2011 2:04 pm   Post subject: Re: "10 Minutes to Writing Your First Ruby Application"

Hi, I believe that this is the article that you are referring to:
http://www.devx.com/RubySpecialReport/Article/34502/0/page/1

1) They have a note on the side of the page, in the 'sidebar', which gives info for Windows users, like yourself:
http://www.devx.com/RubySpecialReport/Article/34502/1763?supportItem=1
That should work if you used that installer (that they mention).

2) When you run the program from the command line, you type in something similar to what they have put at the end of the second page:
ruby launcher.rb launcher.rb
You can see this at the end of page 2, which is at http://www.devx.com/RubySpecialReport/Article/34502/0/page/2
That is a bit of a confusing one that they have chosen. If you wanted to open a file that was called index.html, assuming it's in the same folder as your Ruby file (which is your launcher.rb file), then you would type
ruby launcher.rb index.html
This will open in Firefox, so you will need to have Firefox installed.

3) No. That's just a variable name in the code, just leave it as is. You plug in the file name where I plugged in index.html in the example just above in part 2).

4) app_map is a hash, not a function that is built in to Ruby, just something they've created during the tutorial, to store information in. They made up this name for it probably because the information it stores is which application (app) to use to open each type of file, so in that sense the information they are storing in the app_map hash is a mapping between the file types (based on their endings) and the corresponding program that the computer is being told to run to open that file type.
It's listed in the code on page 2, at http://www.devx.com/RubySpecialReport/Article/34502/0/page/2
If you want to be able to open more file types, add them to the app_map hash in the code.
For example you could add
'txt' => 'notepad',
as one of the lines, perhaps on a new line just after the firefox line would be a good place as then the end comma will be useful to you. Then you could change the example above to launch a txt file instead of a html file; and so on, if you add other file types to the app_map hash.

Maybe see if this gets you further, or post back and let me know if it isn't clear.
Tom.
Display posts from previous:   
   Index -> Programming, Ruby -> Ruby Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: