メソッド
- メソッド → 処理の部品
puts
: 画面に表示する処理sum
: 合計値を計算する処理 etc.
- 処理を実行するために 1 からプログラムを書くのは非効率
- すでに用意された名前が付けられた処理の部品 = メソッドを使うことで記述を簡単にする
- メソッドは定義されたもの以外にも自分で作成できる
例
40.to_s.reverse
# 04
[12, 47, 35].max
# 47
x = 5
puts x.class
# Integer
puts x.to_s.class
# String
String メソッド
Class: String (Ruby 3.1.2)
txt = "hoge"
puts txt.capitalize
# Hoge
puts txt.upcase
# HOGE
puts txt.reverse
# egoh
破壊的メソッド「!」
ticket = [142, 25, 368]
ticket.sort
puts ticket
# 142
# 25
# 368
ticket.sort!
puts ticket
# 25
# 142
# 368
a = "hoge"
# gsub (global substitute) グローバル置換
a.gsub("hoge", "fuga")
puts a
# hoge
b = a.gsub("hoge", "fuga")
puts b
# fuga
a.gsub!("hoge", "fuga")
puts a
# fuga
クラスのオブジェクトが実行できるメソッドを確認する
- レシーバであるオブジェクトで呼び出せるメソッドを一覧表示できる
- 結果は配列で返る
sort
とメソッドチェーンにすると昇順になって見やすくなる
既存クラス
- 整数オブジェクト
1
のレシーバが実行できるメソッド一覧
p 1.methods.sort
# [:!, :!=, :!~, :%, :&, :*, :**, :+, :+@, :-, :-@, :/, :<, :<<, :<=, :<=>, :==, :===, :=~, :>, :>=, :>>, :[], :^, :__id__, :__send__, :abs, :abs2, :allbits?, :angle, :anybits?, :arg, :between?, :bit_length, :ceil, :chr, :clamp, :class, :clone, :coerce, :conj, :conjugate, :define_singleton_method, :denominator, :digits, :display, :div, :divmod, :downto, :dup, :enum_for, :eql?, :equal?, :even?, :extend, :fdiv, :finite?, :floor, :freeze, :frozen?, :gcd, :gcdlcm, :hash, :i, :imag, :imaginary, :infinite?, :inspect, :instance_eval, :instance_exec, :instance_of?, :instance_variable_defined?, :instance_variable_get, :instance_variable_set, :instance_variables, :integer?, :is_a?, :itself, :kind_of?, :lcm, :magnitude, :method, :methods, :modulo, :negative?, :next, :nil?, :nobits?, :nonzero?, :numerator, :object_id, :odd?, :ord, :phase, :polar, :positive?, :pow, :pred, :private_methods, :protected_methods, :public_method, :public_methods, :public_send, :quo, :rationalize, :real, :real?, :rect, :rectangular, :remainder, :remove_instance_variable, :respond_to?, :round, :send, :singleton_class, :singleton_method, :singleton_method_added, :singleton_methods, :size, :step, :succ, :taint, :tainted?, :tap, :then, :times, :to_c, :to_enum, :to_f, :to_i, :to_int, :to_r, :to_s, :truncate, :trust, :untaint, :untrust, :untrusted?, :upto, :yield_self, :zero?, :|, :~]
自作クラス
- 自作クラス Drink のレシーバが実行できるメソッド一覧
class Drink
def name
"coffee"
end
end
drink = Drink.new
p drink.methods.sort
# [:!, :!=, :!~, :<=>, :==, :===, :=~, :__id__, :__send__, :class, :clone, :define_singleton_method, :display, :dup, :enum_for, :eql?, :equal?, :extend, :freeze, :frozen?, :hash, :inspect, :instance_eval, :instance_exec, :instance_of?, :instance_variable_defined?, :instance_variable_get, :instance_variable_set, :instance_variables, :is_a?, :itself, :kind_of?, :method, :methods, :name, :nil?, :object_id, :private_methods, :protected_methods, :public_method, :public_methods, :public_send, :remove_instance_variable, :respond_to?, :send, :singleton_class, :singleton_method, :singleton_methods, :taint, :tainted?, :tap, :then, :to_enum, :to_s, :trust, :untaint, :untrust, :untrusted?, :yield_self]