Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
211710d7
Commit
211710d7
authored
Mar 27, 2014
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1964 from ksss/object-tap
Implement Object#tap
parents
458c18cd
d3ea800c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
0 deletions
+35
-0
mrbgems/mruby-object-ext/mrblib/object.rb
mrbgems/mruby-object-ext/mrblib/object.rb
+19
-0
mrbgems/mruby-object-ext/test/object.rb
mrbgems/mruby-object-ext/test/object.rb
+16
-0
No files found.
mrbgems/mruby-object-ext/mrblib/object.rb
0 → 100644
View file @
211710d7
class
Object
##
# call-seq:
# obj.tap{|x|...} -> obj
#
# Yields <code>x</code> to the block, and then returns <code>x</code>.
# The primary purpose of this method is to "tap into" a method chain,
# in order to perform operations on intermediate results within the chain.
#
# (1..10) .tap {|x| puts "original: #{x.inspect}"}
# .to_a .tap {|x| puts "array: #{x.inspect}"}
# .select {|x| x%2==0} .tap {|x| puts "evens: #{x.inspect}"}
# .map { |x| x*x } .tap {|x| puts "squares: #{x.inspect}"}
#
def
tap
yield
self
self
end
end
mrbgems/mruby-object-ext/test/object.rb
View file @
211710d7
...
@@ -7,3 +7,19 @@ assert('Object#instance_exec') do
...
@@ -7,3 +7,19 @@ assert('Object#instance_exec') do
k
=
KlassWithSecret
.
new
k
=
KlassWithSecret
.
new
assert_equal
104
,
k
.
instance_exec
(
5
)
{
|
x
|
@secret
+
x
}
assert_equal
104
,
k
.
instance_exec
(
5
)
{
|
x
|
@secret
+
x
}
end
end
assert
(
'Object#tap'
)
do
ret
=
[]
(
1
..
10
)
.
tap
{
|
x
|
ret
<<
"original:
#{
x
.
inspect
}
"
}
.
to_a
.
tap
{
|
x
|
ret
<<
"array:
#{
x
.
inspect
}
"
}
.
select
{
|
x
|
x
%
2
==
0
}
.
tap
{
|
x
|
ret
<<
"evens:
#{
x
.
inspect
}
"
}
.
map
{
|
x
|
x
*
x
}
.
tap
{
|
x
|
ret
<<
"squares:
#{
x
.
inspect
}
"
}
assert_equal
[
"original: 1..10"
,
"array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
,
"evens: [2, 4, 6, 8, 10]"
,
"squares: [4, 16, 36, 64, 100]"
],
ret
assert_equal
(
:tap_ok
,
Class
.
new
{
def
m
;
tap
{
return
:tap_ok
};
end
}.
new
.
m
)
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment