Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | Integer | to_r

概要

Integer#to_r

詳細

自身を Rational に変換します。

サンプルコード
# encoding: utf-8
require 'tbpgr_utils'

bulk_puts_eval binding, <<-EOS
1.to_r
0.5.to_r
0.to_r
-2.to_r
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils
出力
1.to_r # => (1/1)
0.5.to_r # => (1/2)
0.to_r # => (0/1)
-2.to_r # => (-2/1)