Skip to content

Instantly share code, notes, and snippets.

@guilleiguaran
Forked from KamilLelonek/monoids.rb
Created February 22, 2019 20:25
Show Gist options
  • Save guilleiguaran/c75b4c2052d2105b6d1832e8c7ebb62d to your computer and use it in GitHub Desktop.
Save guilleiguaran/c75b4c2052d2105b6d1832e8c7ebb62d to your computer and use it in GitHub Desktop.
Monoids examples in Ruby
[1] (pry) main: 0> 1.class
=> Fixnum
[2] (pry) main: 0> 1.method(:+)
=> #<Method: Fixnum#+>
[3] (pry) main: 0> 1 + 0 == 0 + 1
=> true
[4] (pry) main: 0> 2.0.class
=> Float
[5] (pry) main: 0> 2.0.method(:*)
=> #<Method: Float#*>
[6] (pry) main: 0> 2.0 * 1 == 1 * 2.0
=> true
[7] (pry) main: 0> [].class
=> Array
[8] (pry) main: 0> [].method(:+)
=> #<Method: Array#+>
[9] (pry) main: 0> %i(a b) + [] == [] + %i(a b)
=> true
[10] (pry) main: 0> {}.class
=> Hash
[11] (pry) main: 0> {}.method(:merge)
=> #<Method: Hash#merge>
[12] (pry) main: 0> {}.merge(a: 1) == { a: 1 }.merge({})
=> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment