kibiz0r / coalesce

Adds a nil-coalescing method to Ruby, similar to C#'s ?? null-coalescing operator
5 stars 0 forks source link

Unicode is there! Meet `⁇`. #1

Closed akimd closed 6 months ago

akimd commented 6 months ago

Hi,

This is not an issue, just a comment.

I like _? very much: it's very unlikely to be used elsewhere, and easy to type. But, FTR, I have used the same idea, but getting wilder on Unicode. Of course, it's somewhat less convenient to type, but nice looking.

Cheers.

class Object
  def ⁇(x = nil)
    self
  end
end

class NilClass
  def ⁇(x = nil)
    if block_given?
      yield
    else
      x
    end
  end
end

nil.⁇ { puts 1; 1}.⁇{ puts 2; 2}
$ ruby /tmp/coal.rb
1
kibiz0r commented 6 months ago

That's pretty cool!

But I'd worry about coworkers trying to write ?? and wondering why it doesn't work.

If you use font ligatures, ?? may end up rendering the same thing as . I use FiraCode, whose entry for ?? looks almost-but-not-quite identical to .

Without ligatures:

image

With ligatures:

image

That said, if it works for you, go for it!