module PrettyRuby

:count, :detect, :find_index, :find_all, :select, :reject, :collect, :map, :flat_map, :collect_concat, :inject, :reduce, :partition, :group_by, :first, :all?, :any?, :one?, :none?, :minmax, :minmax_by, :member?, :each_with_index, :reverse_each, :each_entry, :each_slice, :each_cons, :each_with_object, :zip, :take, :take_while, :drop, :drop_while, :cycle, :chunk, :slice_before, :slice_after, :slice_when, :chunk_while, :sum, :uniq]

Public Instance Methods

>>(next_proc) click to toggle source
# File lib/pretty_ruby.rb, line 65
def >>(next_proc)
  ->(x) { next_proc.to_proc.(self.to_proc.(x)) }
end
args_given?(args, blk) click to toggle source
# File lib/pretty_ruby.rb, line 261
def args_given?(args, blk)
  blk || !args.empty?
end
doubled() click to toggle source
# File lib/pretty_ruby.rb, line 59
def doubled
  2 * self
end
drop(n) click to toggle source
Calls superclass method
# File lib/pretty_ruby.rb, line 108
def drop(n)
  n >= 0 ? super(n) : take([0, size+n].max)
end
formatted_matrix() click to toggle source
# File lib/pretty_ruby.rb, line 204
def formatted_matrix
  # make ordinary arrays work without special casing them
  #
  matrix = nesting_level > 0 ? self : [self]
  NDimMatrix.new(matrix).to_s
end
init() click to toggle source
# File lib/pretty_ruby.rb, line 137
def init
  drop(-1)
end
map(*args, &blk) click to toggle source

Because Array has a custom implementation of map, instead of just mixing in Enumerable, we need to refine its map separately, even though we use the same code.

Calls superclass method
# File lib/pretty_ruby.rb, line 128
def map(*args, &blk)
  blk = blk ? blk : args.to_proc
  super(&blk)
end
max(other) click to toggle source
# File lib/pretty_ruby.rb, line 47
def max(other)
  self >= other ? self : other
end
max_by(*args, &blk) click to toggle source
Calls superclass method
# File lib/pretty_ruby.rb, line 242
def max_by(*args, &blk)
  return super unless args_given?(args, blk)
  return super(args.first, &blk) if n_given?(args)
  super(&smart_block(args, blk))
end
min(other) click to toggle source
# File lib/pretty_ruby.rb, line 51
def min(other)
  self <= other ? self : other
end
min_by(*args, &blk) click to toggle source
Calls superclass method
# File lib/pretty_ruby.rb, line 248
def min_by(*args, &blk)
  return super unless args_given?(args, blk)
  return super(args.first, &blk) if n_given?(args)
  super(&smart_block(args, blk))
end
n_given?(args) click to toggle source
# File lib/pretty_ruby.rb, line 265
def n_given?(args)
  args && args.first.is_a?(Integer)
end
nesting_level() click to toggle source
# File lib/pretty_ruby.rb, line 162
def nesting_level
  0.step.find { |n| flatten(n).none?(Array) }
end
partial_seqs() click to toggle source
# File lib/pretty_ruby.rb, line 213
def partial_seqs
  self.drop(1).reduce([[self.first]]) do |m, x|
    m << m.last + [x]
  end
end
right_reduce(init = nil, sym = nil, &blk) click to toggle source

TODO: reduce in place

# File lib/pretty_ruby.rb, line 143
def right_reduce(init = nil, sym = nil, &blk)
  reverse.reduce(init, sym, &blk)
end
rscan(fn = nil, &blk) click to toggle source

TODO: make in place

# File lib/pretty_ruby.rb, line 149
def rscan(fn = nil, &blk)
  reverse.scan(fn, &blk)
end
scan(fn = nil, &blk) click to toggle source
# File lib/pretty_ruby.rb, line 153
def scan(fn = nil, &blk)
  no_args = !fn && !blk
  return partial_seqs if no_args
  blk = fn ? fn.to_proc : blk
  self.drop(1).reduce([self.first]) do |m, x|
    m << blk.(m.last, x)
  end
end
smart_block(args, blk) click to toggle source
# File lib/pretty_ruby.rb, line 269
def smart_block(args, blk) 
  blk ? blk : args.to_proc
end
sort_by(*args, &blk) click to toggle source
Calls superclass method
# File lib/pretty_ruby.rb, line 254
def sort_by(*args, &blk)
  return super unless args_given?(args, blk)
  super(&smart_block(args, blk))
end
squared() click to toggle source
# File lib/pretty_ruby.rb, line 55
def squared
  self * self
end
tail() click to toggle source
# File lib/pretty_ruby.rb, line 133
def tail
  drop(1)
end
take(n) click to toggle source
Calls superclass method
# File lib/pretty_ruby.rb, line 112
def take(n)
  n >= 0 ? super(n) : drop([0, size+n].max)
end
to_i() click to toggle source
# File lib/pretty_ruby.rb, line 275
def to_i
  1
end
to_proc() click to toggle source

Need to redefine here so it picks up the other refinements They don't retroactively bubble up inheritance chains. Eg, `scan(:max)` requires that Numeric has our refined `max` method

# File lib/pretty_ruby.rb, line 73
def to_proc
  ->(*args, &blk) do
    receiver = args.first
    if (blk)
      receiver.send(self, &blk)
    else
      args[0] = self
      receiver.send(*args)
    end
  end
end