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.
It's been a year since we lived our Great Bulgarian adventure, 6 months spent living and working in the capital Sofia.
We arrived in mid february to a cold city, and a very unfriendly looking front door to our apartment building, our minds starting to wonder if this was such good idea after all. However, within a couple of hours, and short walk into the city centre for our first meal, our worries had been removed. We would get used to the broken paving tiles and the graffiti. Now we're back in the UK, we miss it, the friendly nature of the local Bulgarians, the great food and the months of blue skies.
Most of time in Bulgaria was spent surrounded by the historical buildings in the centre of the Sofia.
Martenitsa
One of the many traditions that we've kept up after moving back to the UK is the Martenitsa. These simple red and a white wrist bands are given to friends on the 1st of March, Baba Marta day, and are worn until the first buds of spring start to flower. Then you hang the Martenitsa on the tree. By mid spring, not only are all the flowers in full bloom, but every branch in the city centre had Martenitsas hanging or tied on them.
Communism
Although the city has a very mediterranean feel, you cannot ignore the obvious remnants of communism that are scattered around. The big statues to Lenin and Stalin have been moved to a small museum on the outskirts of the city, but the Government buildings and the soviet era tower blocks still remain.
Gradska Gardens
The central Gradska gardens is where we spent many happy hours sitting in the sunshine.