Archive for the ‘ruby’ tag
PDF2PNG – PDF Thumbnail Generator Script
If you need to generate thumbnails of the first pages of alot of PDF documents you can use this tiny ruby script:
http://bit.ly/8T0B1a
I also included smusher support to crush the PNGs after they were created. Just gem install smusher and uncomment the smusher command.
Enjoy!
Popularity: 3% [?]
If Textmate uses the wrong ruby installation…
Quick Tip:
Set a variable called TM_RUBY in Textmate’s “Preferences/Advanced/Shell Variables” menu to the correct path and you can run your script with CMD-R and the correct ruby version.
Popularity: 1% [?]
Awesome Email Presentation
As requested here is the presentation for our awesome email plugin:
Popularity: 2% [?]
Rails “try these” fallback mechanism
If you ever wrote something similar like this (or more complex constructs):
|
|
<%= @jadda.name rescue @jadda.nickname rescue 'unknown' %> |
and then wished you had a construct like try(*these) in prototype? Surrender not, for there’s light at the end of the tunnel!
I present you try these which I found on Chu Yeow’s Blog today.
Here’s the mixins code:
1 2 3 4 5 6 7 8 |
module Kernel def try(*these) raise ArgumentError, 'try requires at least 2 arguments' if these.size <= 1 fallback = these.pop unless these.last.respond_to?(:call) these.each { |candidate| begin return candidate.call rescue next end } fallback || raise(RuntimeError, 'None of the given procs succeeded') end end |
That’s all. Use it like this:
|
|
<%= try(lambda{ @jadda.name }, lambda{ @jadda }, lambda{ 'unknown' }) %>
|
and it will call all these blocks until one will yield no exception or the end of the array is reached. If one yields a result it will end the loop and return the value yielded by that block.
Hope this is useful for someone.
Popularity: 1% [?]
Design Patterns in Ruby
I want to say a few things about a I read lately, called Design Patterns in Ruby.
In this book, Russ Olsen discusses 14 out of the original 23 design patterns described by the GoF. In addition he also talks about 3 not unique but very common patterns in Ruby. Alongside he gives a very nice overview of Ruby for the Ruby beginner and shows some sweet little tricks.
I find the book very comprehensive with the special paragraphs of when to not use a certain pattern, which is really helpful to not get overly enthusiastic with a pattern and try to use it everywhere. Design Patterns are after all solutions that you should use when you found a fitting problem and not when you made a problem fit.
Eventually I created a presentation with a short (not so short, actually) overview of the patterns for my colleagues which I want to share with all of you here.
The presentation on video:
The slides:
Special Thanks
to Jonathan Weiss, who’s introduction and review pointed me to this book.
Popularity: 1% [?]
The splat operator is so pretty
While researching something I stumpled upon this excellent article by Peter Cooper 21 Ruby Tricks You Should Be Using In Your Own Code which also mentions the Splat Operator
The splat operator is a very interesting ruby construct that, if used with care, simplyfies code like the ternary operator to write Array#join
%w{this is a test}.join(", ")
a little different and shorter:
%w{this is a test} * ", "
It’s a silly example, but you get the point.
More on the splat operator can be found at Plan A’s Article – Ruby idioms : The splat operator. or Nick Kallen’s blog post Ruby Pearls Vol.1- The Splat Operator
For an example application I’m currently writing, I needed a simple value-object that holds a few attributes for sending an email. Using this value-object I can have the form fields prepopulated with data, when returning to the form.
Heres the class:
class Htmlmail
attr_accessor :subject, :text, :sender, :recipient
def initialize(args = {})
args.each{ |k,v| p "#{v}"; self.send("#{k}=", v) if self.respond_to?(k) }
end
def to_ary
[sender, recipient, subject, text]
end
end
when implementing the to_ary method (which returns an array), you can use the object like this, where email is a Htmlmail-object:
class Multipart < ActionMailer::Base
def some_mail(email)
sender, recipient, title, message = *email
...
end
end
Popularity: 6% [?]
ActionMailer subject encoding of multipart e-mails
We recently stumbled upon a strange behaviour of ActionMailer when we tried to deliver multipart e-mails with an UTF-8 subject.
One would expect a correctly quoted subject along the lines of:
Subject: =?utf-8?Q?=5bBenachrichtigung_von_imedo=2ede=5d_xyz_hat_Sie_gedr=c3=bcckt?=
but instead we got an encoded string without the charset like this:
Subject: =??Q?=5bBenachrichtigung_von_imedo=2ede=5d_xyz_hat_Sie_gedr=c3=bcckt?=
Our investigations showed, that only multipart mails seem to show this behaviour and setting the charset explicitly in the deliver-method of the mailer doesn’t help.
So to get the task done we are currently using quoted_printable directly to encode the subject line, as all the other parts work just fine.
|
|
subject quoted_printable(("[Benachrichtigung von imedo] XYZ hat sie gedrückt", 'utf-8') |
We will look into submitting a patch for Rails, but for the time beeing this works just fine and we hope that it will help others that encounter the same problem.
Popularity: 1% [?]
