Method: Jig#mult

Defined in:
lib/jig.rb

#mult(rhs) ⇒ Object Also known as: *

call-seq:

jig * count  -> a_jig
jig * array  -> a_jig

With an integer argument, a new jig is constructed by concatenating count copies of self.

three = Jig.new * 3      # => Jig[:___, :___, :___]
puts three.plug('3')     # => "333"

With an array argument, the elements of the array are used to plug the default gap. The resulting jigs are concatenated to form the final result:

require 'yaml'
item = Jig["- ", :___, "\n"]    # => #<Jig: ["- ", :___, "\n"]>
list = item * [1,2,3]           # => #<Jig: ["- ", 1, "\n", "- ", 2, "\n", "- ", 3, "\n"]>
puts list                       # => "- 1\n- 2\n- 3\n"
puts YAML.load(list.to_s)       # => [1, 2, 3]


512
513
514
515
516
517
518
519
520
521
522
# File 'lib/jig.rb', line 512

def mult(rhs)
  case rhs
  when Integer
    raise ArgumentError, "count must be greater than zero" if rhs < 1
    (1...rhs).inject(dup)  { |j,i| j.push(self) }
  when Array
    rhs.inject(Jig.null) { |j,x| j.concat( plug(x) ) }
  else
    raise ArgumentError, "rhs operand for * must be Integer or Array, was #{rhs.class})"
  end
end