mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add Passing Arbitrary Methods As Blocks as a ruby til.
This commit is contained in:
@@ -112,6 +112,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
||||
- [Listing Local Variables](ruby/listing-local-variables.md)
|
||||
- [Override The Initial Sequence Value](ruby/override-the-initial-sequence-value.md)
|
||||
- [Parallel Bundle Install](ruby/parallel-bundle-install.md)
|
||||
- [Passing Arbitrary Methods As Blocks](ruby/passing-arbitrary-methods-as-blocks.md)
|
||||
- [Percent Notation](ruby/percent-notation.md)
|
||||
- [Question Mark Operator](ruby/question-mark-operator.md)
|
||||
- [Squeeze Out The Extra Space](ruby/squeeze-out-the-extra-space.md)
|
||||
|
||||
15
ruby/passing-arbritrary-methods-as-blocks.md
Normal file
15
ruby/passing-arbritrary-methods-as-blocks.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# Passing Arbitrary Methods As Blocks
|
||||
|
||||
Use
|
||||
[`Object#method`](http://ruby-doc.org/core-1.8.7/Object.html#method-i-method)
|
||||
to create a callable `Method` object that can be passed to methods
|
||||
that yield to a block.
|
||||
|
||||
```ruby
|
||||
def inc(x)
|
||||
x + 1
|
||||
end
|
||||
|
||||
[1,2,3].map(&method(:inc))
|
||||
#=> [2,3,4]
|
||||
```
|
||||
Reference in New Issue
Block a user