Skip to content

Instantly share code, notes, and snippets.

@workmad3
Last active August 29, 2015 14:02
Show Gist options
  • Save workmad3/5c19ae87313eb2516c46 to your computer and use it in GitHub Desktop.
Save workmad3/5c19ae87313eb2516c46 to your computer and use it in GitHub Desktop.
class TrueClass
def do(_if: ->{}, _else: ->{})
_if.call
self
end
end
class FalseClass
def do(_if: ->{}, _else: ->{})
_else.call
self
end
end
class TrueClass
def then
yield
self
end
def else
self
end
end
class FalseClass
def then
self
end
def else
yield
self
end
end
@mustmodify
Copy link

Looking at readability:

    model.persist.then do
      # what to do if
      # result was positive
      # maybe add notes or send an email
    end.else do
      # it didn't work out. Handle that.
    end

In terms of readability... I think it's better than a ternary, but the end.else... I wish it could have just been else. I guess using curlies would make it better.

    model.persist.then {
      # what to do if
      # result was positive
      # maybe add notes or send an email
    }.else do
      # what to do if there was some problem
    }

@workmad3
Copy link
Author

for multiline, how is that any more readable than

if model.persist
  # what to do if
  # result was positive
  # maybe add notes or send an email
else
  # it didn't work out. Handle that.
end

? :)

@mustmodify
Copy link

What do you think about 'otherwise' as an alias for else?

find_user_by_email( 'jw@mustmodify.com' ).otherwise do
  puts "Sorry, no such user."
end

maybe I'm overthinking.

@mustmodify
Copy link

It isn't any more readable. But it's more OO or, as you said, smalltalky, which makes me happy.

@mustmodify
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment