From: Mike Stok Date: 2013-07-06T03:59:42+09:00 Subject: Re: what is Vector#map2 ? On 2013-07-05, at 2:49 PM, sto.mar@web.de wrote: > Am 05.07.2013 20:25, schrieb Love U Ruby: >> Hi, >> >> Can anyone give me one example to understand the functionality of >> Vector#map2(http://yard.ruby-doc.org/stdlib-2.0/Vector.html#collect2-instance_method). >> I understand the Vector#map. >> >> require 'matrix' >> >> a = [1, 2, 3, 4] >> m = Vector[*a] # => Vector[1, 2, 3, 4] >> m.map{|i| i * i } # => Vector[1, 4, 9, 16] > > Shouldn't be too difficult to find out by looking at the source, > but anyway: > > 2.0.0p247 :007 > u = Vector[1, 2, 3] > => Vector[1, 2, 3] > 2.0.0p247 :008 > v = Vector[7, 8, 9] > => Vector[7, 8, 9] > 2.0.0p247 :009 > u.collect2(v) {|e1, e2| e1*e2 } > => [7, 16, 27] With pry (and pry-plus) it is easy to get to the source and docs for a method: [1] pry(main)> require 'matrix' => true [2] pry(main)> $ Vector#collect2 From: /Users/mike/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/matrix.rb @ line 1630: Owner: Vector Visibility: public Number of lines: 8 def collect2(v) # :yield: e1, e2 raise TypeError, "Integer is not like Vector" if v.kind_of?(Integer) Vector.Raise ErrDimensionMismatch if size != v.size return to_enum(:collect2, v) unless block_given? Array.new(size) do |i| yield @elements[i], v[i] end end [3] pry(main)> ri Vector#collect2 Vector#collect2 (from ruby core) ------------------------------------------------------------------------------ collect2(v) { |e1, e2| ... } ------------------------------------------------------------------------------ Collects (as in Enumerable#collect) over the elements of this vector and v in conjunction. [4] pry(main)> Hope this helps, Mike -- Mike Stok http://www.stok.ca/~mike/ The "`Stok' disclaimers" apply.