gettext puzzle: unknown type of %string, Solved July 17, 2009
Posted by Paolo Montrasio in Technology and Software.Tags: gettext ruby on rails rake
add a comment
I was running a routine rake updatepo for a Rails application of mine when suddenly I got a unknown type of %string error. The Ruby parsing routines used by gettext weren’t able to understand something that the Ruby interpreter has no problem understanding. There were no hints of the offending lines so finding what to fix in my code was a little troublesome.
A little digging in the error trace got me to the method identify_quotation in the file /usr/lib/ruby/1.8/irb/ruby-lex.rb
I added these lines before RubyLex.fail SyntaxError, "unknown type of %string"
puts "Error parsing Ruby source." puts "Here are the 200 characters following the error point.\n" 200.times do putc getc end puts ""
Running rake again let me see the code immediately following the point where the parser failed. As it turned out the problem was the %m format argument of the calls to strtime embedded inside %() or in multi line strings. Rephrasing those strings let rake updatepo succeed.
Hoptoad and rescue_action_in_public March 3, 2009
Posted by Paolo Montrasio in Technology and Software.Tags: ruby on rails hoptoad
add a comment
Hoptoad is a nice service that lets you send all the exceptions of your Rails application to a central server. That’s a way better that having to scan log files for errors. You alse get an email notification of each event.
Unfortunately hoptoad won’t work straight out of the box (you install it with script/plugin install git://github.com/thoughtbot/hoptoad_notifier.git) if you define rescue_action_in_public, because the latter will intercept the exceptions and prevent hoptoad to get at them.
It took me a while before figuring it out but the fix was easier to find: just add one line at the end of the method.
def rescue_action_in_public(exception) <your code here> notify_hoptoad exception end
That’s it!
February Hackup in Milan February 24, 2009
Posted by Paolo Montrasio in Technology and Software.Tags: hackup facebook ruby on rails arduino connettiva mikamai
4 comments
I partecipated to the Milan Hackup last Sunday and worked with Ivan Vaghi of Mikamai and Emanuel Carnevale to rapidly prototype Let’s Go Out, an application that harvests facebook’s events database and suggests nearby events wherever we are. This guy below is me, but actually the star of the video is Ivan which explains the project (in Italian).
The Hackup wiki explains it in English and here’s a working demo. The code is available on github.
Go to this other blog to see what the other guys at the Hackup did with an Arduino board and another facebook application.
Favicon on Rails July 28, 2008
Posted by Paolo Montrasio in Technology and Software.Tags: ruby on rails favicon
8 comments
Did you ever wanted to generate a <link rel="shortcut icon" href="/favicon.ico" /> tag with an helper to take advange of Rails’asset hosts, that is <link rel="shortcut icon" href="http://static.yourserver.com/favicon.ico" /> ? You’re only three easy steps away from it.
1. Add this code to lib/asset_tag_helper_ext.rb
module ActionView
module Helpers
module AssetTagHelper
def link_tag(url_options = {}, tag_options = {})
href = url_options.is_a?(Hash) ?
url_for(url_options.merge(
:only_path => false)) : url_options
tag(
"link",
"rel" => tag_options[:rel] || nil,
"type" => tag_options[:type] || nil,
"title" => tag_options[:title] || nil,
"href" => compute_public_path(href, "", "")
)
end
end
end
end
2. Require it at the end of config/environment.rb as
require "asset_tag_helper_ext"
3. Add <%= link_tag("/favicon.ico", :rel => "shortcut icon") %> to your layout. Done!
This code has been tested with Rails 2.0.2 and Rails 2.3.2.
Streaming from a Windows desktop January 27, 2008
Posted by Paolo Montrasio in Technology and Software.add a comment
I had some work to do with webcams and video streaming in the weekend. Browsing through forums and web sites I discovered two nice programs and I had some fun playing with them: they are ManyCam and VHScrCap.
ManyCam lets you control multiple webcams and add effects to the video stream.
VhScrCap installs a virtual video driver that adds a video source attached to a region of your screen.
You can use them to create a video stream that mixes your screen with your face. Ever wanted to show your screen to your friends over Skype, or see their screen when they ask your help to fix their computer? Bingo!
This is a little demo that just scratches the surface of what can be done.
Christmas’ Presents December 26, 2007
Posted by Paolo Montrasio in Technology and Software.add a comment
The projects I’ve been working on in the last months are starting to see the daylight. The online collectible card game SkipperMania has been launched in beta this fall and I even found the time to create the web site of my own company, Connettiva. More things will happen in 2008.
Called id for nil, which would mistakenly be 4 June 1, 2007
Posted by Paolo Montrasio in Technology and Software.1 comment so far
Called id for nil, which would mistakenly be 4 — if you really wanted the id of nil, use object_id
Ever got this misterious error message from Ruby on Rails?
It’s pretty clear that you tried to access to the method id of a nil object, but why should it mistakenly be 4???
Try this:
$ irb
irb(main):001:0> nil.id
(irb):1: warning: Object#id will be deprecated; use Object#object_id
=> 4
irb(main):002:0> nil.object_id
=> 4
irb(main):003:0>
So, the number 4 is explained: it’s the object id of the nil object.
And how about that error message, where does it come from?
Go to /usr/local/lib/ruby/gems/1.8/gems or whatever is the path to your rubygems installation, enter the activesupport directory,
open the lib/active_support/whiny_nil.rb file. There it is!
class NilClass
...
def id
raise RuntimeError, "Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id", caller
end
IMHO definitely not a cristal clear error message. It would be, without that “which would mistakenly be 4″ part.
Rails internationalization May 7, 2007
Posted by Paolo Montrasio in Technology and Software.add a comment
This post is about my experiences with Rails’ ri18n and gettext packages. Here are my discoveries:
- Don’t use ri18n because you can only use one language at the time. I can’t think about any real world scenario where that’s acceptable, unless maybe in an intranet application.
- When using gettext, don’t forget to
require "gettext/rails"and callinit_gettext "yourapp"in your application controller, or you’ll get a puzzlingundefined method untranslate_all?error when building the po files. There are no comparable problems with ri18n as all the initialization code is contained into the config files. - Do not expect GetText.update_pofiles to create the .po files in the locale directories on the first run. Just copy the .pot file in each of them, changing the suffix to .po. Those files will be updated on the next runs.
- Gettext doesn’t harvest expressions like #{_(“str”)}, which ri18n’s parser handles happily. This is very bad when working with here documents (var = <<END_OF_STRING)
- gettext tries to guess translations of new messages, based on similar existing ones. It obviously doesn’t make a good job at it (AI’s not here yet) but al least it marks those messages as “fuzzy”. Remember to review them because fuzzy messages won’t make their way into the .mo files (error message: Warning: fuzzy message was ignored.)
Overall ri18n would be a clear winner, superior to gettext in almost every feature. Unfortunately it fails on a show stopper issue, so I have to cope with gettext shortcomings. It’s a hard world…
How to Disable “Restart Now” after a Windows update February 18, 2007
Posted by Paolo Montrasio in Technology and Software.3 comments
How to get rid of that f*** Restart Now popup after a Windows update, when you don’t want to or just can’t restart immediately? In a comment at How to Disable that Blasted “Restart Now” Message from Windows Update Colin Findlay wrote:
- In the Start Menu go to Run type gpedit.msc
- Now a Group Policy editor will open. In this window navigate to: Computer Configuration -> Administrative Template -> Windows Components -> Windows Update
- Double click on No auto-restart for scheduled Automatic Updates installations
- In the settings window Choose Enabled and click OK
- Double click on Re-prompt for restart with scheduled installations
- In the settings window Choose Disabled and click OK
- Close Group Policy Editor
A life saver!
Ironically the change is effective only after a reboot. Meanwhile you can follow the original advice of stopping the Live Update service, which will stop the popup from appearing until the reboot.
This works with Windows XP Pro and Server 2003 only.
PostgreSQL vs. Rails’s fixtures December 3, 2006
Posted by Paolo Montrasio in Technology and Software.1 comment so far
Rails’ fixtures don’t always work well. With PostgreSQL I’ve been experiencing a kind of deadlock, where I can’t say neither fixture :t1, :t2 nor fixture :t2, :t1. I’m using foreign keys that refer from :t1 to :t2, a choice that Rails doesn’t look to like much. I’m getting a runtime error anyway: either t1 can’t be populated before t2, because the elements of t2 must be there in advance; or t2 can’t be deleted because t1 has records that refers to it. Rails can’t figure out the appropriate way to load and delete fixtures, even if the documentation is pretty clear that it should with the right order of fixture declarations. Well, it doesn’t. It should be a bug, but how to work around it? Luckily it’s simple, even if I don’t like it much: enabling ON DELETE CASCADE on each foreign key fixes it.
By the way, this is the first time I run into this problem and it’s the first time that I’m using config.active_record.schema_format = :sql instead of config.active_record.schema_format = :db. I can’t easily go back to :db and check, but it might be that the sql schema format doesn’t let Rails grock the db structure well.