Object#tap のサンプル

okkez e$B$G$9!#e(B

1.8 e$B$Ne(B HEAD e$B$Ge(B Object#tap e$B$N%5%s%W%k%3!<%I$,e(B Syntax
Error e$B$K$J$C$FF0$-$^$;$s!#e(B

% ruby-1.8.x -v sample_tap.rb
ruby 1.8.7 (2008-04-20 patchlevel 5000) [i686-linux]
sample_tap.rb:4: syntax error, unexpected ., expecting $end
.to_a .tap {|x| puts array: #{x.inspect}}
^

% ruby-1.9.x -v sample_tap.rb
ruby 1.9.0 (2007-12-25 revision 0) [i686-linux]
original: 110
array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
evens: [2, 4, 6, 8, 10]
squares: [4, 16, 36, 64, 100]

trunk(r13777e$B$+$ie(B)
e$B$G$O%a%=%C%I8F$S=P$7$N%I%C%H$,A0$KIU$$$F$$$F$bF0$/$h$&$K$J$C$?e(B
e$B$H$$$&OC$re(B ruby-dev:32393
e$B$G8+$?$N$G!"$3$N7o$,e(B1.8e$B$K%P%C%/%]!<%H$5$l$J$$$N$J$ie(B
object.c e$B$N3:EvItJ,$rD>$7$F$*$$$?J}$,$h$$$H;W$$$^$9!#e(B

e$B%a%=%C%I8F$S=P$7$N%I%C%[email protected]$1$G$9$,!#!#!#e(B

Index: object.c

object.c (revision 16122)
+++ object.c (working copy)
@@ -502,10 +502,10 @@

  • 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}"}
      
    • (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}"}
      
    */

At Mon, 21 Apr 2008 17:30:26 +0900,
okkez wrote:

original: 110

    • (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}"}
      
    */

(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}"
}

okkez e$B$G$9!#e(B

e$BJV?.$"$j$,$H$&$4$6$$$^$9!#e(B

(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}"
}

e$BAGKQ$J46$8$G$h$$$H;W$$$^$9!#e(B
[email protected]$h$m$7$/$*4j$$$7$^$9!#e(B

08/04/21 e$B$Ke(B Akinori MUSHA[email protected]
e$B$5$s$O=q$-$^$7$?e(B:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs