Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Ruby—A Diamond of a Programming Language, Part 2 : Page 8

Get ready to dive deeper into the power and elegance of the language lauded as a possible contender to replace current programming languages.


advertisement
A Real-world Ruby Application
With the Ruby you have learned so far, and armed with the Ruby Standard Libraries, you should be ready to build a real world application. To get you started, I have provided a sample application that uses many of the features discussed in this and the previous article. The application reads a simple text file for a set of stock symbols and number of shares that a person owns and looks up the prices for the stocks on a common financial web site (finance.yahoo.com). After retrieving the stock prices, it calculates the value of the person's portfolio by calculating the shares times the price found for each stock.

Beyond demonstrating simple Ruby syntax and class/object construction, this simple application makes use of Ruby’s built in Array class and I/O features to get the symbol and share information from a text file. It then uses Ruby Standard Library classes to gain a connection to the financial web site and retrieve an HTML page containing the stock price for each symbol provided. Finally, it makes extensive use of regular expressions and code blocks to locate or screen scrape the stock price out of the HTML returned by the HTTP connection.

Figure 4. Executing the Stock Retrieval Sample Application: After downloading the contents of the zip file to your file system, use a command prompt to go to the Ruby source code directory and type “ruby fetcher.rb” as shown. The results should look something like what are seen in this picture.



To get the application working, download and unzip the contents of this zip file associated with this article onto your file system. It will create an examples2 folder wherever it is unzipped. There are four Ruby code files (.rb files) and one holdings.txt file in the examples2 directory. The fetcher.rb file serves as the kick off code for executing the stock quote retrieval. To run the sample application, bring up a command prompt and use ruby to run fetcher.rb.

The holding.txt file contains example stock symbols and the theoretical number of shares owned. The current contents of the holding.txt file contain symbols for 3M, Wells Fargo, and Pfizer and look like this:

MMM 11 WFC 20 PFE 5

Feel free to add your own symbols and fictional shares in this file. When you execute the application you should see results that look something like those shown in Figure 4.

Wrap Up
Well, the last article and this article should have you well on your way to successfully exploring and implementing with Ruby. But wait, there is more! Next month, I'll be back with an exploration of Ruby on Rails—the exciting and easy-to-learn Web framework for Ruby.



Jim White is an instructor with Intertech Training. He is also co-author of Java 2 Micro Edition, (Manning).
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap