Archive for November, 2009

A Day Without Talking

Monday, November 23rd, 2009

So I’ve just been to yet another fantastic Railscamp over this weekend and I really enjoyed myself. Well done to the organisers.

As per tradition, however, I have come down with yet-another throat related issue, this time I suspect (using my e-doctor skillz) it’s Laryngitis. On Saturday it was a little bit of effort to speak but I could still do it, yesterday was slightly more effort and today… well: I was putting in as much effort as I would to shout my words in order for them to come out as barely audible-whispers and cracking, broken speech. A friend, Lucas, even gave me the nickname “Squeak” by how often my voice cracked under these conditions.

I have just tried saying “My name is Ryan Bigg” and it comes out in a hoarse whisper. Just tried counting to 10 and I only was able to say 1, 5 and 7 correctly. As an extremely vocal person, this is going to suck bigtime.

So, as part-experiement and part-fucking-laryngitis(??) I’m going to go tomorrow without speaking any words. I will go about my usual routine of getting food + coffee for Chendo from the shops on the way to work, doing my job, getting lunch & dinner, having conversations with people, but all without speaking. I don’t want to exhaust myself by trying to speak. I am already exhausted today, even after 9 hours of some of the most beautiful sleep I’ve ever had.

For prep for this experiment I’ve already ordered dinner from Halim’s (Indian Restaurant) up the road using only pen + paper I bought. I wrote common words + phrases like “I am unable to speak due to Laryngitis. Sorry for the written prompts”, “Yes”, “No”, “Thank you”, “Lamb Rogan Josh”… etc.

This made me think of good-to-have iphone app. A common phrase book for those between us who cannot speak, either due to throat inflammation or those who are mute. You’d have the main screen which would be a 4 x 4 grid of common phrase words, kind of like the home screen, and you could tap them and bring them up on the screen and show it to the person. Then you could add your own as well. I think that would be really useful. What do you think?

Scoping by locales

Sunday, November 8th, 2009

Today in #rubyonrails, kiwinewt asked:

How can I have a model with a text field and have that text field in multiple languages?

To which he meant that he has a model and he wants different versions of text displayed based on whatever the locale is set to. This is quite the common question in the channel and previously I’ve drawn blanks, but today I had a Moment of Clarity +10 and coded up something amazingly simple.

It has two parts. The first is the code in the model:

class Page < ActiveRecord::Base
  def self.with_locale(&block)
    page = scoped_by_locale(I18n.locale.to_s) { block.call }.first
    page ||= scoped_by_locale(I18n.default_locale.to_s) { block.call }.first

    raise ActiveRecord::RecordNotFound, "The page you were looking for does not have a version in #{I18n.locale}" if page.nil?
    
    page
  end
end

And the second is how you use it:

  @page = Page.with_locale { Page.first }

Now if you set I18n.locale in your application and use with_locale it will automatically find records based on that locale.

Where did I put that puts?

Wednesday, November 4th, 2009

This is the question I ask after I’ve just finished a massive debugging session and I run the tests and halfway through there’s something vague like “S3″ printed out. So I do a Cmd+Shift+F looking for that string and of course it doesn’t exist. What’s a guy to do?

Well, at Mocra we put this in our config/environment.rb file (although a better location would be in a required file located somewhere in lib, probably named debug.rb):

# Print the location of puts/p calls so you can find them later
def puts str
  super caller.first if caller.first.index("shoulda.rb") == -1
  super str
end

def p obj puts caller.first super obj end

And when we don’t want it we comment it out. This will give us the exact location of the puts so we can track it down and remove it.