Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
Add example "animals"
Browse files Browse the repository at this point in the history
  • Loading branch information
Braiden Vasco committed Oct 28, 2015
1 parent 7e4924d commit f7ce970
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
46 changes: 46 additions & 0 deletions README.md
Expand Up @@ -10,3 +10,49 @@ Haskell type classes in Ruby.
Examples
--------

```ruby
# This comes from Rust traits example
# http://rustbyexample.com/trait.html

Animal = Typeclass.new a: Object do
fn :name, [:a]
fn :noise, [:a]

fn :talk, [:a] do |a|
"#{name a} says \"#{noise a}\""
end
end

Dog = Struct.new(:name)

Typeclass.instance Animal, a: Dog do
def name(a)
a.name
end

def noise(_a)
'woof woof!'
end
end

Sheep = Struct.new(:name)

Typeclass.instance Animal, a: Sheep do
def name(a)
a.name
end

def noise(_a)
'baaah'
end
end

include Animal

dog = Dog['Spike']
sheep = Sheep['Dolly']

puts talk(dog)
puts talk(sheep)

```
43 changes: 43 additions & 0 deletions examples/animals.rb
@@ -0,0 +1,43 @@
# This comes from Rust traits example
# http://rustbyexample.com/trait.html

Animal = Typeclass.new a: Object do
fn :name, [:a]
fn :noise, [:a]

fn :talk, [:a] do |a|
"#{name a} says \"#{noise a}\""
end
end

Dog = Struct.new(:name)

Typeclass.instance Animal, a: Dog do
def name(a)
a.name
end

def noise(_a)
'woof woof!'
end
end

Sheep = Struct.new(:name)

Typeclass.instance Animal, a: Sheep do
def name(a)
a.name
end

def noise(_a)
'baaah'
end
end

include Animal

dog = Dog['Spike']
sheep = Sheep['Dolly']

puts talk(dog)
puts talk(sheep)

0 comments on commit f7ce970

Please sign in to comment.