Class: Integer

Inherits:
Object show all
Defined in:
lib/sixarm_ruby_ramp/integer.rb,
lib/sixarm_ruby_ramp/integer/rbit.rb

Overview

Integer extensions

Instance Method Summary collapse

Instance Method Details

#mapsObject

Syntactic sugar to yield n times to a block.

## Comparison to Integer#times

Integer#maps is similar to Integer#times except that the output from each call to the block is captured in an array element and that array is returned to the calling code.

Examples:

Generate an array of three random numbers

3.maps{rand}
=> [0.0248131784304143, 0.814666170190905, 0.15812816258206]

Multiply the current index

3.maps{|i| i * 2}
=> [0, 2, 4]

Returns:

  • an array of results



25
26
27
# File 'lib/sixarm_ruby_ramp/integer.rb', line 25

def maps
  return (0...self).map{|item| yield item}
end

#rbit(count = 8) ⇒ Object

Reverse bit.

Example:

0.rbit #=> 0
1.rbit #=> 128
2.rbit #=> 64

The bit count defaults to 8.

Parameters:

  • count (Integer) (defaults to: 8)

    the count of bits to reverse



17
18
19
20
21
22
23
# File 'lib/sixarm_ruby_ramp/integer/rbit.rb', line 17

def rbit(count=8)
  z =  self & 0
  count.times{|i|
    z = z * 2 + self[i]
  }
  z
end