Hi,

I just wrote my first Ruby program and I would be very interested in
some feedback as to style and how to write things more in the
Ruby-way.

I know I could use more methods and/or possibly classes, and that
will happen, but for now I would like to hear what betrays my
programming style coming from other languages.

Thanks,
Esmail



Briefly, this tiny program grabs a HTML for a random date within the
last 10 years from the Astro Picture of the Day (APOD) site
(http://antwrp.gsfc.nasa.gov/apod/archivepix.html).

It then parses the HTML file to extract the name of the larger .jpg or
.gif file (there is usually a smaller version too), generates the
correct URL for the image, and then fires up eog (a Linux program to
display images) to fetch and display the image.

Works .. but it's not pretty, at least in terms of lack of methods
etc. Other ways to improve this?


----------------

#!/usr/bin/ruby
require 'net/http'

# method found on-line, slightly modified

def random_date(years_back=5)
   year = Time.now.year - rand(years_back) - 1
   month = rand(12) + 1
   day = rand(31) + 1
   year = year.to_s.gsub(/20|19/,'')
   sprintf("%02s%02d%02d", year,month,day)
end

r_date=random_date(10) # go back 10 years

puts r_date

#
# get the html file from the apod site
#

text=Net::HTTP.start( 'antwrp.gsfc.nasa.gov', 80 ) do |http|
   http.get( "/apod/ap#{r_date}.html" ).body
end

#
# get the HTML and process contents line by line
#

found_title = false
found_image = false

text.each { |line|

#
# find the title of the current pic
#

if !found_title && line =~ /<TITLE>/i
   title = $'

   puts "----------------------------------------------"
   printf("%s\n", title.strip)
   puts "----------------------------------------------\n\n"

   found_title = true
end



#
# now start searching for the image name
#

if !found_image && line =~ /\.(jpg|gif) *\">/i

   if line.include? "jpg"   # is there a way to case do insensitve comparison here?
     image = $`+".jpg"
   else
     image = $`+".gif"
   end

   url="http://apod.nasa.gov/apod/"
   image=image.gsub(/<a +href="/i,'')
   fetch_url=url+image

   printf("%s\n", fetch_url)

   found_image = true
   system("eog  " + fetch_url + " >& /dev/null &")
end

}