A simple example of ruby

Source: Internet
Author: User
Tags html page stock prices

Based on the current knowledge of Ruby and the help of Ruby Standard library, you should be able to build a real-world application.

In the beginning, I provided a sample application that used a lot of the Ruby features we had discussed. This program reads a simple text file to get a set of stock symbols and the number of shares owned by a person and retrieves the stock price on an ordinary financial site (finance.yahoo.com). After retrieving the stock price, it calculates the portfolio of the person (the stock number x the price of each share).

Rather than just showing a simple ruby syntax and class/object architecture, this simple application also uses Ruby's built-in array class and I/O features to get symbol and share information from a text file. It then uses the Ruby standard library class to connect to the financial site and retrieve an HTML page that contains the stock price corresponding to each symbol provided. Finally, it uses regular expressions and blocks of code to locate or get stock prices from the HTML returned via HTTP connections.

For the application to work, please download and extract the corresponding source files from this article to your file system. After decompression, a Examples2 folder is generated. There are 4 ruby code files (. rb files) and one text file Holdings.txt. The fetcher.rb file is used as the code to perform stock quotes retrieval. To run the sample application, open a command prompt and run the fetcher.rb file with Ruby.

This holding.txt file contains examples of stock symbols and the number of theories owning shares. The current contents of the holding.txt contain symbols corresponding to the 3m,wells Fargo and Pfizer, and appear as follows:

MMM 11

WFC 20

PFE 5

You can add your own symbols and imaginary shares to this file. When you execute this program, you will get similar results as shown in the following diagram.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.