Skip to content

Instantly share code, notes, and snippets.

@wayne5540 wayne5540/elixir.ex
Last active Nov 19, 2017

Embed
What would you like to do?
Ruby implementation for Elixir cond
defmodule Validator do
def validate_age(age) do
cond do
age < 18 -> "Under 18"
age < 21 -> "Under 21"
true -> "Adult"
end
end
end
class Elixir
def cond(*assertions)
return assertions.compact.first
end
def if(assertion, block)
return block.call() if assertion == true
end
end
RSpec.describe Elixir do
let(:elixir) { described_class.new }
describe "cond" do
specify do
result = elixir.cond(
elixir.if(false, -> { return 1 }),
elixir.if(true, -> { return 2 }),
elixir.if(true, -> { return 3 })
)
expect(result).to eq(2)
end
end
describe "if" do
specify do
result = elixir.if(true, -> { 1 })
expect(result).to eq(1)
end
specify do
result = elixir.if(false, -> { 1 })
expect(result).to eq(nil)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.