Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/tsundere/array.rb

Overview

Array extension

Instance Method Summary collapse

Instance Method Details

#binary_search_raw(target, opts = {}) ⇒ Object

binary_search_raw param1: should be comparable against elements in the array if not, provide a block which can be used to do equals returns the index with the target if possible, but will otherwise return the index of the element that is the smallest element larger than the target. It’s raw because it does not check if the array is sorted or not



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/tsundere/array.rb', line 12

def binary_search_raw target, opts={}
	right_flag = true
	right_flag = false if opts[:left]
	if block_given?
		return p_bisearch_internal( 0, count-1, target, right_flag) do |a,b|
			yield a,b
		end # return
	else
		return p_bisearch_internal( 0, count-1, target, right_flag ) do |a,b| 
			a <=> b 
		end
	end
end

#each2(&block) ⇒ Object

each2 is exactly as the name sounds it takes from an array two at a time until it hits the end of the array as such, all the items except for first and last are observed twice



32
33
34
35
36
# File 'lib/tsundere/array.rb', line 32

def each2 &block
	count.times do |n|
		yield self[n], self[n+1] unless n + 1 == count
	end # count n
end