Skip to content

Commit

Permalink
Added Object#do: which allows message cascading style code by calling…
Browse files Browse the repository at this point in the history
… the code with the receiver as the receiver (using Block#call_with_receiver:).
  • Loading branch information
bakkdoor committed Jun 24, 2011
1 parent 6f80a75 commit 7c812a1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/object.fy
Expand Up @@ -542,4 +542,26 @@ class Object {
{ return 0 } if: (self == other)
return 1 # greater or equal to other
}

def do: block {
"""
@block @Block@ to be called in the context of @self.
Helper method that calls @block with @self as the receiver.
This allows message cascading like code, e.g.:
some_complex_object do: {
method_1: arg1
method_2: arg2
method_3: arg3
}
# this is the same as:
some_complex_object method_1: arg1
some_complex_object method_2: arg2
some_complex_object method_3: arg3
"""

block call_with_receiver: self
}
}
14 changes: 14 additions & 0 deletions tests/object.fy
Expand Up @@ -193,4 +193,18 @@ FancySpec describe: Object with: {
self test is == 42
test is == (self test)
}

it: "should call a given block in the context of the receiver (like a message cascade)" for: 'do: when: {
arr = []
arr do: {
<< 1
<< 2
<< 3
select!: 'even?
}
arr is == [2]
arr do: {
is == [2] # same
}
}
}

0 comments on commit 7c812a1

Please sign in to comment.