This is where I got stuck the last time, partially because of a difficulty in setting up the mysql gem. What's a gem? we haven't looked at gem's yet have we? no. The second issue was with the code from the book (Mr Neighbourly's Humble Little Ruby Book), fixing the code took very little time wish I could say the same about setting up the mysql driver.
DBI is the Ruby database package and mr Neighbourly says it’s a lot like perl’s . I can't argue with that. This similarity means, I don't really need to look any further but did so anyway just in case there were caveats and the good news is that there wasn't. However I did run into a strange little bit of code that went like 1.upto(43) I suppose this is yet another way of looping in ruby.
Ruby also supports ORM with the aid of the ActiveRecord package. When using it you classes should be subclasses of ActiveRecord::base and instead of calling DBI::connect you do ActiveRecord::Base.establish_connection after that it's the usual boring stuff.The Library
We have covered strings already in brief at the start but here we are trying to get our hands dirty. Mr Neighbourly says Ruby's string support easily rivals that of perl and other 'power languages' , and dammit that's true. This is certainly more readable than perl and more consistent than PHP (never mind that there are historical reasons for PHP's string api to be so inconsistent). One complaint though is that Ruby should have used ltrim, rtrim and trim instead of lstrip, rstrip and stripRegular Expression
Mr Neighbourly doesn't say as much but i do believe regular expressions in Perl are PCRE. So there is no need to analyse the syntax but the methods do need to be looked at. Let's start with regex match, in addition to the =~ /needle/ Ruby also has var1.match method which does the same thing but returns a MatchData object which allows further manipulation using it's methods like .captures .
scan is a greedy version of match and it returns the matches as an array instead of a MatchData object. If you want to do a replacement you use the .sub methodDate Time
Date and Time functions are the set of functions in any language that I have the most trouble with. I can never remember this stuff. The date and time functions in some languges (like java for example) being rather poorly designed hasn't helped at all. I am not going in too deep here. It will soon be forgotten anyway and there is no way I am going to be able to perform even the simplest date comparision without reaching for the manual.
Suffice to say that there are three classes, aptly named Date, Time and DateTime. However what I really like about Ruby is that you can compare dates using simple operators eg
date1 == date2
date1 < date2
date1 > date2
date1 << 3 # substract 3 months
date1 >> 4 # add 4 months
This is very cool or what! Think of the extremely convoluted way in which you need to handle dates and times in SQL. And SQL is far better than PHP or Java when it comes to date and time functions.
But just in case you like to do things the hard way the strftime function also shows up as a method of the Time classUnit Tests
Not doing unit testing with your PHP code or Java code because you are too lazy to install a unit testing framework. You will be happy to hear that Ruby has one built into the standard library. It's Test::Unit . You create a test case by subclassing Test::Unit::TestCaseAnd it looks like I have reached the end of the book. I certainly cannot claim to have mastered Ruby but I have sufficient knowledge now to read and understand code. Now I am about to set off on a five hour drive to Nuwara Eliya it seems like a good idea to start a learn Ruby on Rails in a day project. Going through what I have written at the start and what I am writing now, it seems I have aquired a taste for Ruby.