Resources for working with ICalendar, Ruby, and Rails

Icalendar is not fun to work with. If only there was a library that would make it easy and pleasant. Alas, that does not seem to be the case, but there are places to start. There are 3 ICalendar libraries available as gems: iCalendar – version 1.1.3 released 3/2010 iCalendar home – rdoc RiCal – …

Continue reading ‘Resources for working with ICalendar, Ruby, and Rails’ »

Simple way to capitalize the first letter of each word in a string using Ruby

I am not infrequently looking to translate a string of either all lowercase or all uppercase characters to a string with the first character of each word capitalized.  I decided to see if I could come up with a quick and easy way to do this in Ruby.  This is what I came up with: …

Continue reading ‘Simple way to capitalize the first letter of each word in a string using Ruby’ »

Ruby Arrays and Hashes and Days of the Week.

I was playing around with different ways to represent the days of the week and their index from 0 to 6 as hashes and arrays.  Here’s what I cam up with: irb(main):001:0> require ‘date’ => true irb(main):002:0> Date::DAYNAMES => [“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”] irb(main):003:0> Date::DAYNAMES.enum_with_index.to_a => [[“Sunday”, 0], [“Monday”, 1], [“Tuesday”, 2], …

Continue reading ‘Ruby Arrays and Hashes and Days of the Week.’ »

2 ways to use LIKE queries with wildcards safely in Rails finders

It is important to sanitize variables that may come from users to prevent SQL injection attacks.  Rails makes this easy by default: Author.find(:all, :conditions=>[‘first_name = ?’, first_name] However this will not work: Author.find(:all, :conditions=>[‘first_name LIKE “%?%”‘, first_name] This will work but is  insecure: Author.find(:all, :conditions=>”first_name LIKE ‘%#{first_name}%'” Solution 1: Author.find(:all, :conditions=>[‘first_name LIKE ?’, “%#{first_name}%”] Solution …

Continue reading ‘2 ways to use LIKE queries with wildcards safely in Rails finders’ »