0
0

[Ruby] クラス定義が重複した場合の挙動

Last updated at Posted at 2023-12-01

Rubyでクラス定義を複数回書いた場合の動きが予想外でした。

結論

Rubyではクラス定義が重複してもエラーは起きません。メソッドは一番最後に定義したものが実行されます。

クラス定義

class C
    def name
        "alice"
    end

    def age
        20
    end
end

class C
    def name
        "bob"
    end
end

実行結果

C.new.name # => bob
C.new.age # => 20
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0