What is Toluu?
Toluu is a free service for sharing the feeds you read and discovering new ones.
Get Invite

Ruby Discoveries and Idioms

A place to share useful Ruby code snippets and ideas about using and improving the Ruby language


Less Ruby, More CocoaDecember 1 2008

I've been doing mostly iPhone development recently, so I haven't had a whole lot to say about Ruby. As such, I'm going to start talking about some Objective-C stuff here. I am still doing Ruby/Rails work, so there will be more of that, too. I'm also hoping to be chosen to speak at either or both of Scotland on Rails and RailsConf.

SSL Certificates and Net::HTTPSMarch 8 2008

I was getting tired of seeing "warning: peer certificate won't be verified in this SSL session" from Ruby's net/https library, so I started looking around for how to get it to actually verify the SSL certificate. I found lots of links on how to tell it not to bother verifying, but it wasn't until I found someone's Japanese blog that I found the clue I was looking for. Now, I don't know Japanese, but I can read Ruby. For the benefit of other English speakers/readers out there, I now present the solution.

First off, I'm giving a full example request using basic authentication (not that GMail uses basic authentication, but this is an example) because I was unable to find a good example elsewhere and made it this far by trial and error. The following will produce the warning I mentioned:

require 'net/http' require 'net/https' require 'uri' url = URI.parse 'https://myname:mypass@mail.google.com/' http = Net::HTTP.new(url.host, url.port) h
CalTerm: Ncurses CalendaringMarch 3 2008

It's no iCal-killer, but I needed something to let me at least view my ical calendars from the terminal. At this point, other than bugs that can be traced to vpim (having to do with recurring events that hit the wrong days, e.g. leap day), it works. It's far from my first Ruby code, but it's my first public Ruby project.

That's the good news. The bad news is that it's definitely still rough around the edges, it isn't packaged in any way, and you'll have to install the vpim and ncurses gems to use it. Grab it from subversion at http://calterm.rubyforge.org/svn/trunk/ or start at the CalTerm home page.

This is sort of a preliminary announcement before I've figured out how I want to package it, so there's no release version.

Enjoy!

The Quest for the UPS APIFebruary 20 2008

This is a bit of a departure for this blog, but it's been a while since I posted and I'm inspired by the hoop-jumping UPS (yes, that UPS) requires for developers to begin using their web service APIs. There are NDAs involved, so this is not about the APIs themselves; it is about getting to the point where one can start working with the APIs. It is presented in the format of a text adventure. Pedantic corrections to format are welcome, since it's been a long time since I've played one.

Welcome to Colossal API Quest! Find the documentation, get authorized to use the development integration environment, and meet the needs of your client to achieve fame and fortune!

  • You are in a maze of twisty little passages, all alike. There is a door to the North.
  • N
  • Welcome to UPS. A GATEKEEPER blocks the door to the North. There are also doors to the East, West, and South.
  • talk to the gatekeeper
  • "Username and password or register?"
  • say register
  • "Tell me everything about yourself. Also, what is your UPS account number?"
  • N
  • "Username and password or register?"
  • E
  • Welcome to UPS. There are several signs here advertising the wonderful web services UPS provides and pointing to the door to the North. There is also a door to the West.
  • N
  • The door is locked.
  • W
  • Welcome to UPS. A GATEKE
Randomizing an Array RevisitedJanuary 7 2008

It was pointed out in a comment on my post about randomizing arrays in Ruby that the sort_by{rand} is O(n log n), and it can be done in linear time, i.e. O(n). This is, of course, correct. Efficiency wasn't my primary concern in the original post, so much as a quick and easy to remember solution. That said, it's worth presenting the more complicated but more efficient algorithm.

I could just give the link to the blog post linked in the comment, but for the convenience of the reader I'll repeat the solution here (with minor changes that make me happier without changing the algorithm):

class Array def shuffle array = dup size.downto 2 do |j| r = rand j array[j-1], array[r] = array[r], array