Swift style conditionals in Ruby

The Swift programming language has a neat feature that guarantees the presence of optional values in conditionals.

// possibleName is an optional variable, it maybe hold a name, or it may be nil
if  (name = possibleName} {
  // name is guaranteed to not be nil
  // name is also declared as a constant, so it will always be available.
  println(name)
}

This method of exacting the optional value into a local constant for use inside the conditional state is a first class construct.

In Ruby you can use these same constructs to a similar effect, although there are pitfalls to using this pattern. You can assign to variables in the conditional check, however, unlike Swift, in Ruby you can only declare throwaway variables, which opens up the possibility of the value mutating later in the code. The new name variable also exists outside the scope of the conditional, polluting the namespace.

IN RUBY

if name = possibleName
  # assuming name is not reassigned, it will be a value
  puts name
  # However, as name is a variable, we can reassign it
  name = 123
  puts name #=> 123
end
# name is still available outside
puts name # => 123

USING TAP

A Ruby solution to this would wrap the conditional in a block, that would provide the variable to only the block. Object#tap does exactly this, but unfortunately, this approach does not protect us from the possibleName being nil, so we'd still need to check that possibleName has a value.

possibleName.tap do |name|
  puts name if name != nil
end
puts name #=> NameError

WRITE YOUR OWN

Because Ruby is the awesome language that it is, we can extend the behaviour of the Object class. Here, I've defined an Object#tap! method, like Object#tap it yields itself to the supplied block, unlike Object#tap, it won't yield if 'self' is nil.

class Object
  def tap!
    yield self if block_given? && self != nil
  end
end

"Some string".tap! do |name|
puts name
end
# => Some string

nil.tap! do |name|
puts name
end
#=> this does nothing

This gets us pretty close to the swift unboxed value example. The harder part is making this temporary variable immutable, and that's a problem for another day.

About the Author

Phil Balchin is a full-time software developer at Zendesk, previously at Heroku/Salesforce, and Kyan, as well as a part-time photographer living in Guildford, Surrey, UK.

facebook.com/phil.balchin | instagram.com/maniacalrobot | last.fm/users/maniacalrobot | picfair.com/maniacalrobot | maniacalrobot.tumblr.com | twitter.com/maniacalrobot
© 2011–2024 Philip Balchin | About | sitemap | Colophon | RSS | π