{"id":1285,"date":"2010-02-17T22:53:19","date_gmt":"2010-02-17T21:53:19","guid":{"rendered":"http:\/\/www.devco.net\/?p=1285"},"modified":"2010-06-27T19:12:20","modified_gmt":"2010-06-27T18:12:20","slug":"few_rubyisms","status":"publish","type":"post","link":"https:\/\/www.devco.net\/archives\/2010\/02\/17\/few_rubyisms.php","title":{"rendered":"Few Rubyisms"},"content":{"rendered":"

While looking at some bits of other peoples Ruby code I came across a few shortcuts and interesting structures worth mentioning.<\/p>\n

Exception handling shortcut<\/h1>\n

First up a shortcut to catch exceptions thrown by a method:<\/p>\n

<\/p>\n

\r\ndef say_foo\r\n   puts \"foo\" if doit\r\nrescue Exception\r\n   puts \"#fail\"\r\nend\r\n<\/pre>\n

<\/code><\/p>\n

So since we didn’t define doit<\/em> this will raise an exception, which will be handled. Nice shortcut to avoid an extra inner begin \/ rescue<\/em> block.<\/p>\n

sprintf equivelant<\/h1>\n

Ruby supports sprintf style string building in a handy little shortcut:<\/p>\n

<\/p>\n

\r\nputs \"%2.6f\\n%d\" % [1, 1]\r\n<\/pre>\n

<\/code><\/p>\n

This produces:<\/p>\n

<\/p>\n

\r\n$ ruby test.rb\r\n1.000000\r\n1\r\n<\/pre>\n

<\/code><\/p>\n

Get a value from a hash with default for non existing<\/h1>\n

This is really nice, I’ve written way too many constructs like this:<\/p>\n

<\/p>\n

\r\nfoo.include?(:bar) ? bar = foo[:bar] : bar = \"unknown\"\r\n<\/pre>\n

<\/code><\/p>\n

One option that I was told about was this:<\/p>\n

<\/p>\n

\r\nbar = foo[:bar] || \"unknown\"\r\n<\/pre>\n

<\/code><\/p>\n

But that does not work if you had false<\/em> in the hash, or maybe even nil.<\/p>\n

Turns out there’s an awesome shortcut for this:<\/p>\n

<\/p>\n

\r\nbar = foo.fetch(:bar, \"unknown\")\r\n<\/pre>\n

<\/code><\/p>\n

Reloading a class<\/h1>\n

Sometimes you want to reload a class you previously loaded with require<\/em>. I have the need in my plugin manager for mcollective. There’s a simple fix by simply using Kernel#load to load the .rb file, each time you load it the file will be reloaded from disk.<\/p>\n

<\/p>\n

\r\nirb(main):001:0> load \"test.rb\"\r\n=> true\r\nirb(main):002:0> Foo.doit\r\nfoo\r\nirb(main):003:0* load \"test.rb\"\r\n=> true\r\nirb(main):004:0> Foo.doit\r\nfoo foo\r\n<\/pre>\n

<\/code><\/p>\n

In between lines 2 and 3 I edited the file test.rb<\/em> and just reloaded it, the changes on disk reflected in the current session. The main difference is that you need to supply the full file name and not just test<\/em> like you would with require<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"

While looking at some bits of other peoples Ruby code I came across a few shortcuts and interesting structures worth mentioning. Exception handling shortcut First up a shortcut to catch exceptions thrown by a method: def say_foo puts “foo” if doit rescue Exception puts “#fail” end So since we didn’t define doit this will raise […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","footnotes":""},"categories":[7],"tags":[121,85,13],"_links":{"self":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/1285"}],"collection":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/comments?post=1285"}],"version-history":[{"count":11,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/1285\/revisions"}],"predecessor-version":[{"id":1515,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/1285\/revisions\/1515"}],"wp:attachment":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/media?parent=1285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/categories?post=1285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/tags?post=1285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}