Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
f5fb1307
Commit
f5fb1307
authored
May 16, 2019
by
KOBAYASHI Shuji
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix `Rational#==`
parent
9b604abc
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
72 additions
and
7 deletions
+72
-7
mrbgems/mruby-rational/mrbgem.rake
mrbgems/mruby-rational/mrbgem.rake
+0
-1
mrbgems/mruby-rational/mrblib/rational.rb
mrbgems/mruby-rational/mrblib/rational.rb
+25
-4
mrbgems/mruby-rational/test/rational.rb
mrbgems/mruby-rational/test/rational.rb
+47
-2
No files found.
mrbgems/mruby-rational/mrbgem.rake
View file @
f5fb1307
...
@@ -3,7 +3,6 @@ MRuby::Gem::Specification.new('mruby-rational') do |spec|
...
@@ -3,7 +3,6 @@ MRuby::Gem::Specification.new('mruby-rational') do |spec|
spec
.
author
=
'mruby developers'
spec
.
author
=
'mruby developers'
spec
.
summary
=
'Rational class'
spec
.
summary
=
'Rational class'
spec
.
add_dependency
'mruby-metaprog'
,
core:
'mruby-metaprog'
spec
.
add_dependency
'mruby-object-ext'
,
core:
'mruby-object-ext'
spec
.
add_dependency
'mruby-object-ext'
,
core:
'mruby-object-ext'
spec
.
add_dependency
'mruby-numeric-ext'
,
core:
'mruby-numeric-ext'
spec
.
add_dependency
'mruby-numeric-ext'
,
core:
'mruby-numeric-ext'
end
end
mrbgems/mruby-rational/mrblib/rational.rb
View file @
f5fb1307
class
Rational
<
Numeric
class
Rational
<
Numeric
# Override #<, #<=, #>, #>= in Numeric
prepend
Comparable
def
initialize
(
numerator
=
0
,
denominator
=
1
)
def
initialize
(
numerator
=
0
,
denominator
=
1
)
@numerator
=
numerator
@numerator
=
numerator
@denominator
=
denominator
@denominator
=
denominator
...
@@ -66,6 +69,24 @@ class Rational < Numeric
...
@@ -66,6 +69,24 @@ class Rational < Numeric
end
end
end
end
def
<
=>
(
rhs
)
case
rhs
when
Fixnum
return
@numerator
<=>
rhs
if
@denominator
==
1
rhs
=
Rational
(
rhs
)
when
Float
return
to_f
<=>
rhs
end
case
rhs
when
Rational
(
@numerator
*
rhs
.
denominator
-
@denominator
*
rhs
.
numerator
)
<=>
0
when
Numeric
return
rhs
<=>
self
else
nil
end
end
def
negative?
def
negative?
numerator
.
negative?
numerator
.
negative?
end
end
...
@@ -86,15 +107,15 @@ def Rational(numerator = 0, denominator = 1)
...
@@ -86,15 +107,15 @@ def Rational(numerator = 0, denominator = 1)
end
end
[
Fixnum
,
Float
].
each
do
|
cls
|
[
Fixnum
,
Float
].
each
do
|
cls
|
[:
+
,
:
-
,
:
*
,
:/
,
:
=
=
].
each
do
|
op
|
[:
+
,
:
-
,
:
*
,
:/
,
:
<=>
,
:==
,
:<
,
:<=
,
:
>
,
:
>
=
].
each
do
|
op
|
cls
.
instance_exec
do
cls
.
instance_exec
do
original_operator_name
=
"__original_operator_
#{
op
}
_rational"
original_operator_name
=
"__original_operator_
#{
op
}
_rational"
alias_method
original_operator_name
,
op
alias_method
original_operator_name
,
op
define_method
op
do
|
rhs
|
define_method
op
do
|
rhs
|
if
rhs
.
is_a?
Rational
if
rhs
.
is_a?
Rational
Rational
(
self
).
send
(
op
,
rhs
)
Rational
(
self
).
__send__
(
op
,
rhs
)
else
else
send
(
original_operator_name
,
rhs
)
__send__
(
original_operator_name
,
rhs
)
end
end
end
end
end
end
...
...
mrbgems/mruby-rational/test/rational.rb
View file @
f5fb1307
...
@@ -3,6 +3,16 @@ def assert_rational(real, exp)
...
@@ -3,6 +3,16 @@ def assert_rational(real, exp)
assert_float
real
.
denominator
,
exp
.
denominator
assert_float
real
.
denominator
,
exp
.
denominator
end
end
def
assert_equal_rational
(
exp
,
r1
,
r2
)
if
exp
assert_operator
(
r1
,
:==
,
r2
)
assert_not_operator
(
r1
,
:
!=
,
r2
)
else
assert_not_operator
(
r1
,
:==
,
r2
)
assert_operator
(
r1
,
:
!=
,
r2
)
end
end
assert
'Rational'
do
assert
'Rational'
do
r
=
5
r
r
=
5
r
assert_equal
Rational
,
r
.
class
assert_equal
Rational
,
r
.
class
...
@@ -55,3 +65,38 @@ assert 'Rational#/' do
...
@@ -55,3 +65,38 @@ assert 'Rational#/' do
assert_rational
Rational
(
9
,
8
)
/
4
,
Rational
(
9
,
32
)
assert_rational
Rational
(
9
,
8
)
/
4
,
Rational
(
9
,
32
)
assert_float
Rational
(
20
,
9
)
/
9.8
,
0.22675736961451246
assert_float
Rational
(
20
,
9
)
/
9.8
,
0.22675736961451246
end
end
assert
'Rational#==, Rational#!='
do
assert_equal_rational
(
true
,
Rational
(
1
,
1
),
Rational
(
1
))
assert_equal_rational
(
true
,
Rational
(
-
1
,
1
),
-
1
r
)
assert_equal_rational
(
true
,
Rational
(
13
,
4
),
3.25
)
assert_equal_rational
(
true
,
Rational
(
13
,
3.25
),
Rational
(
4
,
1
))
assert_equal_rational
(
true
,
Rational
(
-
3
,
-
4
),
Rational
(
3
,
4
))
assert_equal_rational
(
true
,
Rational
(
-
4
,
5
),
Rational
(
4
,
-
5
))
assert_equal_rational
(
true
,
Rational
(
4
,
2
),
2
)
assert_equal_rational
(
true
,
Rational
(
-
4
,
2
),
-
2
)
assert_equal_rational
(
true
,
Rational
(
4
,
-
2
),
-
2
)
assert_equal_rational
(
true
,
Rational
(
4
,
2
),
2.0
)
assert_equal_rational
(
true
,
Rational
(
-
4
,
2
),
-
2.0
)
assert_equal_rational
(
true
,
Rational
(
4
,
-
2
),
-
2.0
)
assert_equal_rational
(
true
,
Rational
(
8
,
6
),
Rational
(
4
,
3
))
assert_equal_rational
(
false
,
Rational
(
13
,
4
),
3
)
assert_equal_rational
(
false
,
Rational
(
13
,
4
),
3.3
)
assert_equal_rational
(
false
,
Rational
(
2
,
1
),
1
r
)
assert_equal_rational
(
false
,
Rational
(
1
),
nil
)
assert_equal_rational
(
false
,
Rational
(
1
),
''
)
end
assert
'Fixnum#==(Rational), Fixnum#!=(Rational)'
do
assert_equal_rational
(
true
,
2
,
Rational
(
4
,
2
))
assert_equal_rational
(
true
,
-
2
,
Rational
(
-
4
,
2
))
assert_equal_rational
(
true
,
-
2
,
Rational
(
4
,
-
2
))
assert_equal_rational
(
false
,
3
,
Rational
(
13
,
4
))
end
assert
'Float#==(Rational), Float#!=(Rational)'
do
assert_equal_rational
(
true
,
2.0
,
Rational
(
4
,
2
))
assert_equal_rational
(
true
,
-
2.0
,
Rational
(
-
4
,
2
))
assert_equal_rational
(
true
,
-
2.0
,
Rational
(
4
,
-
2
))
assert_equal_rational
(
false
,
3.3
,
Rational
(
13
,
4
))
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment