Archive | Ruby

Ruby, Part 2 — Dates

Jan 20th, 2006No Comments

I have a theory that we should judge a language by how it handles dates out of the box. I’m not saying that dates are trivial (because they aren’t) but sooner or later we are going to have to write some code that compares dates, figures dates, etc. Since we are all going to do it sometime, the language should make it easy to do out of the box.

With that litmus test, Java would not score well, since it makes you jump through hoops (Calendar, anyone? ). Perl would do even worse, since all it has is localtime. Python wasn’t much better until they introduced datetime. Though strptime is still in the time module and there isn’t a very direct way to change a time tuple to a datetime (though it’s possible).

Enough ranting about other languages — this is about Ruby and how it does dates and time (I just call them “dates”). Ruby has it’s own DateTime module as well as Date and Time. I played with Date a little but then moved to DateTime — this is probably what I would do more often. The strptime format works well — it uses the same format characters as C’s strptime, which is a good thing (and, really, the easiest for Ruby developers to write). You can add integers to a DateTime object, print them, etc. Very nice stuff.

Here is the script I wrote to play around with this:


#!/usr/bin/ruby

require 'date'

datestr=`date`
mydate = DateTime.strptime(datestr,"%a %b %d %H:%M:%S %Z %Y")

puts mydate

bday = DateTime.new(1974,9,18)

puts bday
## these aren't seconds, I don't think . .
diff=mydate-bday
puts "I am #{diff} secs old"

tomorrow = mydate + 1
puts tomorrow

My First Ruby Program

Jan 19th, 20064 Comments

In danger of seeming to jump on the bandwagon, I’ve spent part of my afternoon writing my first Ruby program. First off — does this mean I’m leaving Python behind? No. Hey, it’s an up-and-coming language that seems to be catching on. I know quite a few people doing Rails stuff. So why not see what the fuss is about?

I think everyone has their own little pet problem they try to solve with a new programming language. Mine is the Sieve of Eratosthenes, which is an easy enough concept to remember yet complex enough so you have to dig a bit deeper into arrays, etc, then a normal tutorial.

This little script accepts a command-line argument for the maxium number. If it can parse that to a string, or no argument is given, it uses 20. My first thoughts are below:

#!/usr/bin/ruby

if ARGV[0] and ARGV[0].to_i>0
  Max=ARGV[0].to_i
else
  Max=20
end

puts "find primes from 1 to "+Max.to_s
nums = Array.new
primes=Array.new
composite=Array.new

for x in 2..Max
    nums.push(x)
end

nums.each do |x|

   if not composite.find{|i| i==x}
      primes.push(x)
      composite+=nums.find_all{|i| i>x && i%x==0}
  end

end

puts primes
  • I think the find and find_all methods for Enumerations should be easier to do if you want to do something easy — say, just find a value in an array.
  • I really like the unified object approach. It seems that all basic objects have .to_s and most have .to_i.
  • One of my attempts was to used a step in range, but that seemed little weird. Luckily, find_all did it for me.
  • It seemed silly that I had to populate the nums array at all. I’m sure that someone else will show me something better.

Overall, I’m impressed. I expected sometime Perl-ish but instead got something quite a bit different. And better, I do believe. I’m also sure that others will find better ways to do this — let me know if you have something.

Page 3 of 3«123