Ruby

Null Pattern for Ruby Strings

No surprises - Expect what you should expect

Andy Huynh

1 minute read

Super simple: you check the presence of an object to do work on it. We see this in code all the time. Common presence check pattern if params[:coupon_code].present? params[:coupon_code].upcase else params[:coupon_code] # Let’s say this would return nil end This dance isn’t special and we can avoid it with a Ruby trick by typecasting the string. irb > nil.to_s => "" > "".upcase => "" Since typecasting nil to a string will always return a blank string, we can call string methods safely.

Don't Deal With Ambiguous Data

Mutate Ruby Objects or Don't. Never combine the two.

Andy Huynh

1 minute read

Everytime an object or variable is invoked by a non mutating action, the object remains untouched. This is particularly relevant when passing objects around via method calls. If the passed hash gets mutated along the chain, receiving class method require more responsibility to handle ambiguous data. New hash keys could be introduced or removed which changes your expectation of what you’re getting. This is cautionary coding we want to avoid. Seldom is the case to mutate objects holding state or data.

Stack Level Too Deep - Not Too Deep For Me

Guard against Double Loading when Monkeypatching with Alias Methods

Andy Huynh

2 minute read

Before we discuss how to guard against a double load when monkeypatching with an alias method, know that a stack-level-too-deep error is a good indicator your method has already been defined. It loaded already and is trying to load again. This is commonplace in background jobs where a class might get loaded more than once. It’s unfortunate for your monkeypatched method being called because you’ll go through an infinite loop which leads to the error above.

Wrap Your Head Around Memoization

ˌmemərəˈzāSHən | noun

Andy Huynh

1 minute read

Ruby has a memoization operator, ||=. In a nutshell, we use it for caching the result of a method to avoid querying it over and over. Our application can function faster as a result! There’s a particular use case to keep in mind. Let’s say you’re finding an account and want their credit card. It’s common to query and check if your resource (account), is present first. We’re being prudent so we don’t encounter a NoMethodError on NilClass, a nil account.

Is it Ruby, or Rails Code?

Distinguish Between The Two With This Trick

Andy Huynh

1 minute read

A common mistake among new developers is asking premature questions. Top shelf developers are best utilized as a last resort. Rails developers are expected to read and understand a TON of code. Hopefully you’ll be reading good, idiomatic Ruby/Rails code. Jostling whether a class is Ruby or Rails is a common bump in the road. There are clues to distinguish the difference. For example, when I come across ActiveSupport::OrderedSOptions in an application, it’s blatant Rails code with ActiveSupport as a clear indicator.

Build Confidence Using Enumerables

Ruby Enumerable Nil Guard

Andy Huynh

1 minute read

Ruby is notorious for its enumerable library. I frequent Ruby’s Array#map to iterate and modify a collection of data. A common occurrence is looping over a collection and getting an undefined method _ for nil:NilClass. > irb > > arr = [1, 2, 3] > arr.map { |x| x * 5 } => [5, 10, 15] > arr << nil > arr << 5 => [1, 2, 3, nil, 5] > arr.map { |x| x * 5 } NoMethodError: undefined method ‘*’ for nil:NilClass from (irb):in block in irb_binding from (irb):in `map’ Map has trouble looping over nil values.

Simple Authentication with Cuba, Shield and Mongoid

A tutorial on logging in users safely

Andy Huynh

4 minute read

User authentication should be secure and easy to understand. We’ll create a basic authentication interface using tools built by the boys at Openredis: Cuba, Shield and Mongoid. If you haven’t already, check out Openredis. It’s awesome. Audience Hopefully you’re familiar with a Ruby framework. If not, Cuba is a fantastic gateway framework to understand the way of the web and how Ruby gets us writing expressive and readable code. Tools Cuba: Popular microframeworks like Sinatra and Ramaze will forever have a place in Rubyland.