Cast variable to string unless it's nil in Ruby 2.3+
irb(main):001:0> var = nil
=> nil
irb(main):002:0> var&.to_s
=> nil
irb(main):003:0> var = 5
=> 5
irb(main):004:0> var&.to_s
=> "5"
Written by msz
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Related Tags
#ruby
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#