Delegating methods in Ruby

Sometimes, when constructing a compound object, we are interested in exporting functionality while retaining encapsulation. For example, suppose we have a Secretary class: class Secretary def send_fax(destination, fax_contents) puts 'Sending fax "%s" to %s' % [fax_contents, destination] end def answer_call(call) # ... end # ... end Our Secretary provides a lot of useful functionality, that our Boss class would like to have. Boss would like to be able to say that he can send a fax, without having the user explicitly request his Secretary beforehand....

October 18, 2008 路 1 min 路 Ohad Lutzky

Three things I didn't know Ruby does

Edit: I was misled! Illustrated here. Hints below. >> def inspect_x_and_y(x,y); puts "x: %p, y: %p" % [x, y]; end => nil >> inspect_x_and_y(y={"hello" => "world"},x=[1,2,3]) x: {"hello"=>"world"}, y: [1, 2, 3] The bits I didn鈥檛 know about: "Format strings using a %% sign, %s, %s!" % [ "just like in python", "but with arrays" ] The %p formatting character is the same as inspect. You can call methods with method_name(param2=val2, param1=val1), also like in python....

July 25, 2008 路 1 min 路 Ohad Lutzky

Gettext oddities with Ruby

I was having a lot of trouble with gettext in Ruby, mostly due to lacking documentation. Here are some useful things I figured out while writing TTime. I ended up having a single gettext_settings.rb, included from every file which uses gettext. Here it is (with some extra notes) #!/usr/bin/ruby begin require 'gettext' require 'pathname' include GetText # This fixes a swarm of problems on Windows GetText.locale.charset = "UTF-8" # Ruby's gettext acts in a sane # method - add a path to the set of paths # scanned....

July 20, 2008 路 1 min 路 Ohad Lutzky