Class: Array

Inherits:
Object show all
Defined in:
lib/roebe/core/array.rb

Overview

#

require ‘roebe/core/array.rb’

#

Instance Method Summary collapse

Instance Method Details

#by_step(which_step_to_do = 100, limes = 1000, start_position = 0) ⇒ Object

#

by_step

This method is used by e. g. the method called random_by_step().

Usage example:

Array.new.by_step(15,800)
Array.new.by_step(15,400,-200)
#


148
149
150
151
152
153
154
155
156
# File 'lib/roebe/core/array.rb', line 148

def by_step(
    which_step_to_do =  100,
    limes            = 1000,
    start_position   =    0
  )
  array = [] 
  start_position.step(limes, which_step_to_do) { |i| array << i}
  array
end

#cut_down_to(n_elements = 1) ⇒ Object

#

cut_down_to

$ary.cut_down_to(1)

This method can be used if you want to reduce an array to n elements. It will start at the first element.

Another way would be:

a = [ "a", "b", "c" ];a.slice!(-1); a; # => ["a", "b"]
array.slice! 0..n

If you need a random way to cut down an array, make use of the method called random_cut_down_to().

Usage example:

[1,2,3,4,5,6,7].cut_down_to(1) # => [1]
#


118
119
120
# File 'lib/roebe/core/array.rb', line 118

def cut_down_to(n_elements = 1)
  return self[0 ... n_elements]
end

#downcaseObject

#

downcase

Downcase each entry in an array.

Example:

require 'roebe/core/array.rb'; array = %w( ABC DEF GHI ); puts array.downcase
#


64
65
66
# File 'lib/roebe/core/array.rb', line 64

def downcase
  self.map {|entry| entry.downcase }
end

#downcase!Object

#

downcase!

#


71
72
73
# File 'lib/roebe/core/array.rb', line 71

def downcase!
  self.each { |element| element.downcase! }
end

#has_duplicate?Boolean

#

has_duplicate?

#

Returns:

  • (Boolean)


298
299
300
# File 'lib/roebe/core/array.rb', line 298

def has_duplicate?
  ! unique?
end

#lrotate!Object

#

lrotate!

Rotates an array to the left

Usage example would be:

['a','b','c'].lrotate!

Result is:

['c','a','b']
#


256
257
258
# File 'lib/roebe/core/array.rb', line 256

def lrotate!
  self.unshift(self.pop)
end

#output(input = '') ⇒ Object

#

output

Just outputs the array. Also applies an optional argument ” in front of the Array.

#


239
240
241
# File 'lib/roebe/core/array.rb', line 239

def output(input = '')
  self.each { |entry| puts input.to_s+entry.to_s }
end

#random_by_step(which_step_to_do = 100, limes = 1000, start_position = 0) ⇒ Object

#

random_by_step

Randomly selects one entry from a stepped Array.

Usage examples:

Array.new.random_by_step
Array.new.random_by_step(5,100) # => 35
require 'roebe/extensions'; [1,2,3].random_by_step(3,30) # => 3
#


168
169
170
171
172
173
174
# File 'lib/roebe/core/array.rb', line 168

def random_by_step(
    which_step_to_do =  100,
    limes            = 1000,
    start_position   =    0
  )
  by_step(which_step_to_do,limes,start_position).sample
end

#random_cut_down_to(n = 1) ⇒ Object

#

random_cut_down_to

See above, but it does random cutting instead.

[1,2,3,4,5,6].random_cut_down_to(2)
#


130
131
132
133
134
135
136
137
# File 'lib/roebe/core/array.rb', line 130

def random_cut_down_to(n = 1)
  array_stores_original_data = self
  array_stores_new_data = []
  n.times {
    array_stores_new_data << array_stores_original_data.sample
  }
  return array_stores_new_data
end

#remove(this_character = '@') ⇒ Object

#

remove

This method will remove a sub-character from every member of the Array.

Usage example:

['@a','b','c'].remove('@')
#


185
186
187
# File 'lib/roebe/core/array.rb', line 185

def remove(this_character = '@')
  self.map {|_| _.gsub(/#{this_character}/,'')}
end

#return_duplicatesObject

#

return_duplicates

Will return keys which are duplicates (or in other words, all entries which occur more than once). ” is ignored though.

Example:

['foo', 'bar', 'ble', 'foo', 'ble', 'go','yo','',''].return_duplicates
#


198
199
200
201
202
203
204
205
# File 'lib/roebe/core/array.rb', line 198

def return_duplicates
  return_value = []
  hash = Hash.new(0)
  self.each { |word| hash[word] += 1 }
  hash.select {|key, value| return_value << key if value > 1 and ! key.empty?}
  return_value = [] if return_value.empty?
  return return_value
end

#reverse_uniqObject

#

reverse_uniq

Returns an array containing no uniques.

Usage example:

require 'roebe/core/array.rb'; %w( a b c a d e e e ).reverse_uniq # => ["a", "e", "e"]
#


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/roebe/core/array.rb', line 42

def reverse_uniq
  uniq_array = self.uniq
  array_clone = self.dup
  self.each { |tmp_entry|
    if uniq_array.include?(tmp_entry)
      uniq_array.delete_at(uniq_array.rindex(tmp_entry))
      array_clone.delete_at(array_clone.rindex(tmp_entry))
    end
  }
  return array_clone
end

#shortest?Boolean

#

shortest?

Will return the shortest string of an Array. However, it will only return one match - keep this in mind.

["aaa","asds","ab","be"].shortest? # => "be"
#

Returns:

  • (Boolean)


214
215
216
217
218
# File 'lib/roebe/core/array.rb', line 214

def shortest?
  match = nil
  match = self.inject { |prev, e| prev.length < e.length ? prev : e }
  return match
end

#sort_by_alphabeticalObject

#

sort_by_alphabetical

This method will put numerical values at the end.

Usage Example:

%w(z a b Beb GAOia ZZink  bulgaria 2 33 4).sort_by_alphabetical
=> ["a", "b", "z", "2", "4", "33"]
#


269
270
271
# File 'lib/roebe/core/array.rb', line 269

def sort_by_alphabetical
  self.sort_by { |s| [ s.to_i, s.downcase ] }
end

#split(n_chunks = 5) ⇒ Object

#

split

This method will split up an Array into chunks, by making use of .each_slice().

To test this method, do:

[$array+$array+$array].flatten.split(3)
#


229
230
231
# File 'lib/roebe/core/array.rb', line 229

def split(n_chunks = 5)
  self.each_slice(n_chunks).to_a
end

#strip!Object

#

strip!

Apply .strip! on each element of an Array.

Invocation example:

[" aa","b","c"].strip!
#


329
330
331
# File 'lib/roebe/core/array.rb', line 329

def strip!
  self.map! {|_| _.strip}
end

#strip_newlines!Object

#

strip_newlines!

This method will remove all newlines from all elements of a given Array.

Usage example:

x = ["ljkgljkg","jklfjklf\n"]; x.strip_newlines!
x = ["ljkgljkg\njklsajlksad"]; x.strip_newlines!
#


87
88
89
90
91
92
93
94
95
# File 'lib/roebe/core/array.rb', line 87

def strip_newlines!
  self.each { |element|
    if element.is_a? Array
      element.each { |inner_element| inner_element.tr!("\n",'') }
    else
      element.tr!("\n", '')
    end
  }
end

#sumObject

#

sum

The method .sum() on class Array will return the sum of all Array elements. These are assumed to be Integers.

Usage examples:

[1, 1, 2, 3, 5, 8,24,1,1,1].sum   # => 47
[1, 1.5, 2, 3, 5, 8,24,1,1,1].sum # => 47.5
#


343
344
345
# File 'lib/roebe/core/array.rb', line 343

def sum
  inject(0) { |s, v| s += v }
end

#unique?Boolean

#

unique?

Checks whether an array is “unique” or not. It will be unique only if all of its members are unique. I.e. if we find a duplicate then we can not be unique.

#

Returns:

  • (Boolean)


309
310
311
312
313
314
315
316
317
# File 'lib/roebe/core/array.rb', line 309

def unique?
  old_size    = self.size
  unique_size = self.uniq.size
  if unique_size == old_size
    true
  else
    false
  end
end

#upcaseObject

#

upcase

Return an Array with all members upcased.

Invokes block once for each element of self. Creates a new array containing the values returned by the block. $ary.upcase

Invocation example:

require 'roebe/extensions'; $array.upcase
#


285
286
287
288
289
290
291
292
293
# File 'lib/roebe/core/array.rb', line 285

def upcase
  self.map {|entry|
    if entry.nil?
      entry
    else
      entry.upcase
    end
  } 
end

#verbose_countObject

#

verbose_count

Will output the array in a self-counting manner.

Usage example:

require 'roebe/core/array.rb'; %w( a b c a d e e e ).verbose_count
#


26
27
28
29
30
# File 'lib/roebe/core/array.rb', line 26

def verbose_count
  self.each_with_index { |item, index|
    puts "#{index+1}: #{item}"
  }
end