`alias`랑 `alias_method` 둘 중 어느걸 쓰는게 맞나요?

조회수 1675회

같은 클래스 내에서 메소드를 alias할건데 어느 쪽을 쓰는게 맞나요?

이름만 보면 alias_method를 쓰는게 맞는 것 같은데 alias를 써도 크게 문제 없는 걸 보면 왜 굳이 2개를 나눠놨나..하는 생각도 듭니다

1 답변

  • 좋아요

    0

    싫어요
    채택 취소하기

    먼저 말하자면,

    • alias는 키워드 입니다.
    • alias_method는 메소드로, 재정의될 수 있습니다.

    alias

    Gives alias to methods or global variables. Aliases can not be defined within the method body. The aliase of the method keep the current definition of the method, even when methods are overridden. Making aliases for the numbered global variables ($1, $2,...) is prohibited. Overriding the builtin global variables may cause serious problems.

    method나 전역변수의 alias를 만듭니다. method body안에서는 정의할 수 없습니다. method가 overrid되더라도 해당 메소드의 alias는 유지됩니다. numbered 전역변수($1, $2, ...)의 alias는 금지되어있습니다. 내장된 전역변수를 alias하는건 심각한 문제를 야기할 수도 있습니다

    #alias_method

    • alias_method(new_name, old_name) → self

    Makes new_name a new copy of the method old_name. This can be used to retain access to methods that are overridden.

    old_name 메소드를 복사한 new_name 메소드를 만듭니다. overrid된 메소드도 접근할 수 있습니다.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)