Class: Array

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

Instance Method Summary collapse

Instance Method Details

#rotate(n = 1) ⇒ Object



5
6
7
8
# File 'lib/modalsupport/array.rb', line 5

def rotate(n=1)
  n = (n % self.size)
  n>0 ? self[n..-1] + self[0...n] : self
end

#rotate!(n = 1) ⇒ Object



11
12
13
# File 'lib/modalsupport/array.rb', line 11

def rotate!(n=1)
  self.replace(self.rotate(n))
end

#to_h(default = nil, &block) ⇒ Object

create a hash from an array of [key,value] tuples you can set default or provide a block just as with Hash::new Note: if you use [key, value1, value2, value#], hash will be [value1, value2, value#]



34
35
36
37
38
39
# File 'lib/modalsupport/array.rb', line 34

def to_h(default=nil, &block)
  # code by Stefan Rusterholz fixed for Ruby 1.9.2; see http://www.ruby-forum.com/topic/138218
  hash = block_given? ? Hash.new(&block) : Hash.new(default)
  each { |(key, *value)| hash[key]=value.size>1 ? value : value.first }
  hash
end