Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 RUBY


  Use downcase! with caution in Ruby

Ruby provides ! to change state of some object in place. Hence if you see some functions have ! appended, it means the state of the caller of the function is expected to be changed. This is a very interesting Ruby feature. But sometimes one should be cautious when using this kind of functions because you would get unexpected behavior if using improperly.Let's take an example of String#downcase!. According to the documentation.Downcases the contents of str, returning nil if no changes were made. Note: case replacement is effective only in ASCII region.If there is a String pl...

3,852 0       RUBY EXCLAMATION MARK DOWNCASE


  A plugin to update last_error in Delayed Job

delayed_job is a process based asynchronous task processing gem which can be ran at background. It will fork the specified number of processes to execute the tasks asynchronously. The task status is usually stored in the database so that it can be easily integrated into a Rails application where asynchronous job execution is desired.Normally when a job fails to execute or error occurs, it would save the error into the database with the column last_error. Ideally all these will be handled by the delayed_job gem itself, we no need to worry about anything. However, in some extreme cases, we ...

3,796 0       RUBY RUBY ON RAILS DELAYED JOB LAST_ERROR


  Method chaining and lazy evaluation in Ruby

Method chaining has been all the rage lately and every database wrapper or aything else that’s uses queries seems to be doing it. But, how does it work? To figure that out, we’ll write a library that can chain method calls to build up a MongoDB query in this article. Let’s get started!Oh, and don’t worry if you haven’t used MongoDB before, I’m just using it as an example to query on. If you’re using this guide to build a querying library for something else, the MongoDB part should be easy to swap out.Let’s say we’re working with a...

3,133 0       RUBY IMPLEMENTATION METHOD CHAINING LAZY EVALUATION


  Don't Overload #nil?

There’s a popular post on Hacker News about writing confident code by, among other things, overloading Object#nil? and returning “null objects” instead of nil itself.DO NOT DO THIS.In Ruby, all objects (except nil itself) coerce to the boolean value true. Your object will be nil and true at the same time. Bad things will happen. Your coworkers will cry. Sad people from around the world will ask bewildering questions on your mailing list.Here’s what happens:1234567891011class NullObject def nil? true endendo = NullObject.newo.nil? #=>...

2,835 0       RUBY OBJECT #NIL OVERLOAD


  Never create Ruby strings longer than 23 characters

Looking at things through a microscopesometimes leads to surprising discoveriesObviously this is an utterly preposterous statement: it’s hard to think of a more ridiculous and esoteric coding requirement. I can just imagine all sorts of amusing conversations with designers and business sponsors: “No… the size of this <input> field should be 23… 24 is just too long!” Or: “We need to explain to users that their subject lines should be less than 23 letters…” Or: “Twitter got it all wrong… the 140 limit should have been 23!â€...

2,465 0       RUBY OPTIMIZATION SPECIFICATION STRING INTERPRETER 23