# クラスメソッドを定義
class TestData
class << self
# 一般的なメソッド定義
def hoge1
"hoge1"
end
# 1行でメソッド定義(文字列を返す)
def hoge2() "hoge2" end
define_method(:hoge3) { "hoge3" }
# 1行でメソッド定義(ハッシュを返す)
def hoge4() { foo: "foo", bar: "bbb" } end
define_method(:hoge5) { { foo: "foo", bar: "bbb" } }
end
end
# メソッドを呼び出す
puts TestData.hoge1 # => hoge1
puts TestData.hoge2 # => hoge2
puts TestData.hoge3 # => hoge3
puts TestData.hoge4[:foo] # => foo
puts TestData.hoge5[:foo] # => foo
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme