Very First Ruby Script

require ‘uri’
require ‘net/http’
require ‘tk’

@symbol = TkVariable.new
@value = TkVariable.new

def getvalue(s)
uri = URI.parse(“http://finance.yahoo.com/q?s=#{s}”)
response = Net::HTTP.get_response(uri)
body = response.body
snip = body[body =~ /Last Trade/,70]
a = snip.split(//)
a[4]
end

def searchvalue
@value.value = getvalue(@symbol.value)
end

pads = {‘padx’ => 10, ‘pady’ => 5}
root = TkRoot.new {title “Stock”}
TkLabel.new(root) {text “Enter stock symbol”; pack(pads)}
TkEntry.new(root, “textvariable” => @symbol).pack(pads)
TkLabel.new(root, “textvariable” => @value).pack(pads)
search_b = TkButton.new(root) {text “Search”; pack(pads)}
search_b.command {searchvalue}
exit_b = TkButton.new(root) {text “Exit”; pack(pads)}
exit_b.command {exit}

Tk.mainloop

Explore posts in the same categories: Uncategorized

One Comment on “Very First Ruby Script”

  1. Anonymous Says:

    nice stock ticker.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: