Class: Fixnum

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

Overview

#

All about class Fixnum

#

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_primes?(end_where = 50, start_where = 0) ⇒ Boolean

#

Fixnum.all_primes?

Call like so:

Fixnum.all_primes?
#

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/roebe/core/fixnum.rb', line 54

def self.all_primes?(end_where = 50, start_where = 0)
  result = []
  start_where.upto(end_where.to_i).each { |x|
    result << x if x.is_prime?
  }
  return result
end

Instance Method Details

#eql(inp) ⇒ Object

#

eql

#


80
81
82
83
# File 'lib/roebe/core/fixnum.rb', line 80

def eql(inp)
  return true if inp == self
  # puts "No, #{self} is not equal #{inp}." if inp != self
end

#hex_reverseObject

#

hex_reverse

Does the reverse of String#hex method.

16.hex_reverse # => “0x10” to convert it again do: “0x10”.hex # => 16

#


151
152
153
# File 'lib/roebe/core/fixnum.rb', line 151

def hex_reverse
  return '0x'+self.to_s(16)
end

#is_prime?Boolean

#

is_prime?

Usage example:

2.is_prime?      # => true
113.is_prime?    # => true
123457.is_prime? # => true
#

Returns:

  • (Boolean)


44
45
46
# File 'lib/roebe/core/fixnum.rb', line 44

def is_prime?
  ((('1' * self) =~ /^1$|^(11+?)\1+$/) == nil)
end

#modify(by_this_amount = 1) ⇒ Object

#

modify

This Fixnum method modifies the value of self, ie adds to it, or subtracts from it.

See the following usage examples to understand why this is a convenient method:

15.modify('+55')    # => 70
20.modify('+1')     # => 21
15.modify('-44442') # => -44427
#


98
99
100
101
102
103
104
105
106
107
108
# File 'lib/roebe/core/fixnum.rb', line 98

def modify(by_this_amount = 1)
  by_this_amount = by_this_amount.to_s
  case by_this_amount[0,1]
  when '+'
    return (self+by_this_amount[1, by_this_amount.size].to_i)
  when '-'
    return (self-by_this_amount[1, by_this_amount.size].to_i)
  else # default is add 
    return (self+by_this_amount.to_i)
  end
end

#to_celsiusObject

#

to_celsius

From Fahrenheit to Celsius.

Celsius = (Fahrenheit - 32) * 5/9

68.to_celsius
#


17
18
19
# File 'lib/roebe/core/fixnum.rb', line 17

def to_celsius
  return 5/9.to_f * (self - 32.to_f)
end

#to_fahrenheitObject

#

to_fahrenheit

20 °C = 68 °F von celsius zu fahrenheit Fahrenheit = Celsius * 9/5 + 32

Usage example:

require 'roebe/extensions'; 20.to_fahrenheit
#


31
32
33
34
# File 'lib/roebe/core/fixnum.rb', line 31

def to_fahrenheit
  fahrenheit = (self.to_f * 9 / 5) + 32
  return fahrenheit
end

#to_rgbObject

#

to_rgb

1555.to_rgb

#


115
116
117
118
# File 'lib/roebe/core/fixnum.rb', line 115

def to_rgb
  a, b = divmod(65536)
  return b.divmod(256).unshift(a)
end

#to_sekundenObject Also known as: to_sekunde, stunde_zu_sekunde

#

to_sekunden

in stunden 5.to_sekunden

#


136
137
138
139
# File 'lib/roebe/core/fixnum.rb', line 136

def to_sekunden
  tmp = self.to_f
  return (tmp * STUNDE.to_f).to_i
end

#to_stundenObject Also known as: to_stunde

#

to_stunden

in sekunden 3600.to_stunden

#


125
126
127
128
# File 'lib/roebe/core/fixnum.rb', line 125

def to_stunden
  tmp = self.to_i
  return (tmp / STUNDE.to_f)
end

#to_verbose_integerObject

#

to_verbose_integer

returns a string.

Also check out in class String, to_verbose_integer

Example usage for this method:

4000000.to_verbose_integer # => "4_000_000"
#


72
73
74
75
# File 'lib/roebe/core/fixnum.rb', line 72

def to_verbose_integer
  tmp = self.to_s.reverse.scan(/\d{1,3}/).join('_').reverse
  tmp
end