examples/enumerating.rb in protocol-0.8.2 vs examples/enumerating.rb in protocol-0.9.0
- old
+ new
@@ -1,7 +1,15 @@
-require 'protocol/core'
+require 'protocol'
+Enumerating = Protocol do
+ # Iterate over each element of this Enumerating class and pass it to the
+ # _block_.
+ def each(&block) end
+
+ include Enumerable
+end
+
begin
class FailAry
def initialize
@ary = [1, 2, 3]
end
@@ -9,12 +17,13 @@
def each()
end
conform_to Enumerating
end
+ puts "Should have thrown Protocol::CheckFailed!"
rescue Protocol::CheckFailed => e
- e.to_s # => "Enumerating#each(0&): expected a block argument for FailAry"
+ p e # => "Enumerating#each(0&): expected a block argument for FailAry"
end
class Ary
def initialize
@ary = [1, 2, 3]
@@ -25,13 +34,13 @@
end
conform_to Enumerating
end
-Ary.new.map { |x| x * x } # => [1, 4, 9]
-Ary.conform_to?(Enumerating) # => true
-Ary.new.conform_to?(Enumerating) # => true
+puts Ary.new.map { |x| x * x }.inspect + " ([1, 4, 9])"
+puts Ary.conform_to?(Enumerating).to_s + " (true)"
+puts Ary.new.conform_to?(Enumerating).to_s + " (true)"
Enumerating.check_failure :none
class FailAry2
def initialize
@@ -41,7 +50,7 @@
def each() end
conform_to Enumerating
end
-FailAry2.conform_to?(Enumerating) # => false
-FailAry2.new.conform_to?(Enumerating) # => false
+puts FailAry2.conform_to?(Enumerating).to_s + " (false)"
+puts FailAry2.new.conform_to?(Enumerating).to_s + " (false)"