Class: Numeric

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
mrblib/numeric.rb,
mrbgems/mruby-complex/mrblib/complex.rb,
mrbgems/mruby-rational/mrblib/rational.rb,
mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb

Overview

Numeric

ISO 15.2.7

Direct Known Subclasses

Complex, Rational

Instance Method Summary collapse

Methods included from Comparable

#<, #<=, #==, #>, #>=, #between?, #clamp

Instance Method Details

#+@Object

Returns the receiver simply.

ISO 15.2.7.4.1



11
12
13
# File 'mrblib/numeric.rb', line 11

def +@
  self
end

#-@Object

Returns the receiver's value, negated.

ISO 15.2.7.4.2



19
20
21
# File 'mrblib/numeric.rb', line 19

def -@
  0 - self
end

#absObject

Returns the absolute value of the receiver.

ISO 15.2.7.4.3



27
28
29
30
31
32
33
# File 'mrblib/numeric.rb', line 27

def abs
  if self < 0
    -self
  else
    self
  end
end

#integer?Boolean

call-seq:

num.integer?  ->  true or false

Returns true if num is an Integer.

1.0.integer? #=> false 1.integer? #=> true

Returns:

  • (Boolean)


59
60
61
# File 'mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 59

def integer?
  false
end

#negative?Boolean

call-seq:

negative? -> true or false

Returns true if self is less than 0, false otherwise.

Returns:

  • (Boolean)


46
47
48
# File 'mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 46

def negative?
  self < 0
end

#nonzero?Boolean

call-seq:

nonzero?  ->  self or nil

Returns self if self is not a zero value, nil otherwise; uses method zero? for the evaluation.

Returns:

  • (Boolean)


22
23
24
25
26
27
28
# File 'mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 22

def nonzero?
  if self == 0
    nil
  else
    self
  end
end

#positive?Boolean

call-seq:

positive? -> true or false

Returns true if self is greater than 0, false otherwise.

Returns:

  • (Boolean)


36
37
38
# File 'mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 36

def positive?
  self > 0
end

#to_cObject

call-seq:

num.to_c -> complex

Returns the value as a complex.

1.to_c       #=> (1+0i)
-1.to_c      #=> (-1+0i)
1.0.to_c     #=> (1.0+0i)
3.14159.to_c #=> (3.14159+0i)


256
257
258
# File 'mrbgems/mruby-complex/mrblib/complex.rb', line 256

def to_c
  Complex(self, 0)
end

#to_rObject

call-seq:

num.to_r -> rational

Returns the value as a rational.

1.to_r        #=> (1/1)
(1+2i).to_r   #=> (1+2i)/1)
nil.to_r      #=> TypeError


63
64
65
# File 'mrbgems/mruby-rational/mrblib/rational.rb', line 63

def to_r
  Rational(self, 1)
end

#zero?Boolean

call-seq:

zero? -> true or false

Returns true if zero has a zero value, false otherwise.

Of the Core and Standard Library classes, only Rational and Complex use this implementation.

Returns:

  • (Boolean)


11
12
13
# File 'mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 11

def zero?
  self == 0
end