プログラミング未経験者の学習日記

私が学習した内容のまとめサイトです。

【Ruby入門】数値オブジェクトを使おう!

 

 数値に関するオブジェクト

「32」や「4.8」など整数や浮動小数点数のことを数値といいます。

 

クラスを調べる方法

Rubyでは下記のように「*.class」と書くとどのクラスに属しているか調べることができます。

p 32.class

p 4.8.class

実行結果は下記となります。「32」は数値オブジェクトの「Fixnum」クラス、「4.8は」数値オブジェクトの「Float」クラスに属していることがわかります。

Fixnum

Float

メソッドを調べる方法

Rubyでは下記のように「*.methods」と書くとどんなメソッドを持っているか調べることができます。

p 32.methods

p 4.8.methods

 実行結果は下記となります。(記号も含まれていますがこれらもメソッドです。)

[:%, :&, :*, :+, :-, :/, :<, :>, :^, :|, :~, :-@, :**, :<=>, :<<, :>>, :<=, :>=, :==, :===, :[], :inspect, :size, :succ, :to_s, :to_f, :div, :divmod, :fdiv, :modulo, :abs, :magnitude, :zero?, :odd?, :even?, :bit_length, :to_int, :to_i, :next, :upto, :chr, :ord, :integer?, :floor, :ceil, :round, :truncate, :downto, :times, :pred, :to_r, :numerator, :denominator, :rationalize, :gcd, :lcm, :gcdlcm, :+@, :eql?, :singleton_method_added, :coerce, :i, :remainder, :real?, :nonzero?, :step, :positive?, :negative?, :quo, :arg, :rectangular, :rect, :polar, :real, :imaginary, :imag, :abs2, :angle, :phase, :conjugate, :conj, :to_c, :between?, :instance_of?, :public_send, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :private_methods, :kind_of?, :instance_variables, :tap, :define_singleton_method, :is_a?, :singleton_method, :extend, :to_enum, :enum_for, :=~, :!~, :respond_to?, :freeze, :display, :object_id, :send, :method, :public_method, :nil?, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :trust, :untrusted?, :methods, :protected_methods, :frozen?, :public_methods, :singleton_methods, :!, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__]

[:%, :*, :+, :-, :/, :<, :>, :-@, :**, :<=>, :<=, :>=, :==, :===, :eql?, :inspect, :to_int, :to_s, :to_i, :to_f, :hash, :coerce, :divmod, :fdiv, :modulo, :abs, :magnitude, :zero?, :floor, :ceil, :round, :truncate, :positive?, :negative?, :quo, :nan?, :infinite?, :finite?, :next_float, :prev_float, :to_r, :numerator, :denominator, :rationalize, :arg, :angle, :phase, :+@, :singleton_method_added, :div, :i, :remainder, :real?, :integer?, :nonzero?, :step, :rectangular, :rect, :polar, :real, :imaginary, :imag, :abs2, :conjugate, :conj, :to_c, :between?, :instance_of?, :public_send, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :private_methods, :kind_of?, :instance_variables, :tap, :define_singleton_method, :is_a?, :singleton_method, :extend, :to_enum, :enum_for, :=~, :!~, :respond_to?, :freeze, :display, :object_id, :send, :method, :public_method, :nil?, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :trust, :untrusted?, :methods, :protected_methods, :frozen?, :public_methods, :singleton_methods, :!, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__]

四則演算

四則演算は下記となります。

p 10 + 3   #足算。結果は13。
p 10 - 3 #引算。結果は7。
p 10 * 3 #掛算。結果は30。
p 10 ** 3  #乗算。結果は1000。
p 10 / 3 #割算。結果は3(小数点以下の計算されない)
p 10.0 / 3.0 #割算。結果は3.33・・・(小数点以下も計算されます)
p 10 % 3  #余りを求める。結果は1。

Rubyでは分数の計算も可能で Rationalというものを使用します。

p Rational(2,5) + Rational(3,4)  #分数の計算はRational()を使用。
p 2/5r + 3/4r          #Rational()の省略型。

四捨五入/切捨て

Floatクラスは四捨五入や小数点以下の切り上げ切り捨てができます。

p 52.5.round #四捨五入。結果は53。
p 52.5.floor   #小数点以下切り捨て。結果は52。
p 52.5.ceil  #小数点以下切り上げ。結果は53。