Trivial Example: Compose a meet & greet in Ruby
class Greeter
class << self
def meet(person, &block)
yield person
end
# Greet everyone except Johnny
def greet(person, &block)
if person == "Johnny"
yield "Can't talk right now, Johnny"
else
yield "Hello #{person}!"
end
end
end
end
Greeter.meet "Johnny" do |person|
Greeter.greet person do |message|
puts message
end
end
Greeter.meet "Brooke" do |person|
Greeter.greet person do |message|
puts message
end
end
The same functionality in Elixir:
defmodule Greeter do
def meet(person), do: person
def greet("Johnny"), do: "Can't talk right now, Johnny"
def greet(person), do: "Hello #{person}!"
end
"Johnny" |> Greeter.meet |> Greeter.greet |> IO.puts
"Brooke" |> Greeter.meet |> Greeter.greet |> IO.puts
As a fan of both Ruby and Elixir, what you have is interesting but not quite equivalent and is therefore not quite fair to Ruby.
This is because I am noticing that you are defining blocks in Ruby that you aren't using. Hence, it seems as the equivalent Ruby to what you're doing in Elixir would look like:
So you can see the that LOC for the two versions are much closer together.
That being said, you do gain efficiency with Elixir's pattern matching, and I do like the
|>
syntax in Elixir.