Like this article? We recommend
Sample Programs
I think that the programs written in Ruby will demonstrate its benefits most clearly. The following are small sample programs:
# tiny "cat"
while line = gets()
print line
end
# print "From " line from stdin
STDIN.each{|line| print line if /^From / =~ line}
# rename filenames specified in args into lowercase names
ARGV.each{|path| File.rename(path, path.downcase)}
# Ruby/Tk
require 'tk'
TkButton.new(nil, 'text'=>'hello', 'command'=>'exit').pack
Tk.mainloop
# socket example - httpget
# usage: ruby httpget.rb http://host/path
require 'socket'
host = "localhost" # default values
port = 80
path = "/"
# process URL (%r!..! means regular expression)
if %r!http://(.*?)(?::(\d+))?(/.*)! =~ ARGV[0]
host = $1
port = Integer($2) if $2
path = $3
end
s = TCPsocket::open(host, port)
# send out HTTP header
s.print "GET #{path} HTTP/1.0\r\n\r\n"
print s.read