Skip to content
Browse files

Allow 1-arg blocks passed to Object#do:

When passing a 1-arg block to Object#do: it behaves like Object#tap:
  • Loading branch information...
1 parent 025d80f commit 483730df5209b08d0a0b378babcf962e74cee653 @bakkdoor committed Jun 2, 2012
Showing with 29 additions and 1 deletion.
  1. +13 −1 lib/object.fy
  2. +16 −0 tests/object.fy
View
14 lib/object.fy
@@ -627,9 +627,21 @@ class Object {
some_complex_object method_1: arg1
some_complex_object method_2: arg2
some_complex_object method_3: arg3
+
+ If you pass it a block with 1 argument this method behaves exactly like @Object#tap:@
+
+ Example:
+ some_complex_object do: @{
+ method_1: arg1
+ method_2: arg2
+ method_3: arg3
+ }
"""
- block call_with_receiver: self
+ match block arity {
+ case 0 -> block call_with_receiver: self
+ case _ -> block call: [self]
+ }
self
}
View
16 tests/object.fy
@@ -215,6 +215,22 @@ FancySpec describe: Object with: {
}
}
+ it: "calls a given block with self if the block takes an argument" with: 'do: when: {
+ arr = []
+ arr do: @{
+ << 1
+ << 2
+ << 3
+ select!: 'even?
+ } . is: [2]
+
+ arr do: @{
+ is: [2] # same
+ }
+
+ 2 do: @{ inspect } . is: 2
+ }
+
it: "calls a given block with the receiver before returning itself" with: 'tap: when: {
10 + 2 tap: |x| {
x is: 12

0 comments on commit 483730d

Please sign in to comment.
Something went wrong with that request. Please try again.