Module: DS::ArrayX
- Defined in:
- lib/ds/ext/ext.rb
Instance Method Summary collapse
-
#push_uniq(e) ⇒ Object
Pushes element e only if it is not already in the array.
-
#sorted?(order = :any) ⇒ Boolean
Checks if array is already sorted.
-
#tail ⇒ Object
Tail.
Instance Method Details
#push_uniq(e) ⇒ Object
Pushes element e only if it is not already in the array. Returns index of elemnt e.
22 23 24 25 26 27 28 29 |
# File 'lib/ds/ext/ext.rb', line 22 def push_uniq e if include? e index e else push e size-1 end end |
#sorted?(order = :any) ⇒ Boolean
Checks if array is already sorted.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/ds/ext/ext.rb', line 5 def sorted?(order=:any) return true if size < 2 case order when :asc (size-2).times{ |i| return false if self[i] > self[i+1] } when :desc (size-2).times{ |i| return false if self[i] < self[i+1] } else return (self[0] < self[1])? sorted?(:asc) : sorted?(:desc) end true end |
#tail ⇒ Object
Tail
32 33 34 |
# File 'lib/ds/ext/ext.rb', line 32 def tail self[1..-1] end |