Ruby에서 모든 메소드를 메소드하는 방법은 무엇입니까?
특정 개체가 액세스 할 수있는 모든 방법을 어떻게 사용할 수 있습니까?
@current_user
응용 프로그램 컨트롤러에 정의 된 개체 가 있습니다 .
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
보기 파일에서 내가 사용할 수있는 방법을보고 싶습니다. 특히, :has_many
협회가 제공 하는 방법을보고 싶습니다 . (무엇 :has_many
을 제공 해야 할 것인지 확인하고 싶습니다.)
다음은 기본 객체 클래스에없는 사용자 클래스의 메소드 목록입니다.
>> User.methods - Object.methods
=> ["field_types", "maximum", "create!", "active_connections", "to_dropdown",
"content_columns", "su_pw?", "default_timezone", "encode_quoted_value",
"reloadable?", "update", "reset_sequence_name", "default_timezone=",
"validate_find_options", "find_on_conditions_without_deprecation",
"validates_size_of", "execute_simple_calculation", "attr_protected",
"reflections", "table_name_prefix", ...
참고 methods
클래스 및 클래스 인스턴스에 대한 방법입니다.
다음은 ActiveRecord 기본 클래스에없는 사용자 클래스의 메소드입니다.
>> User.methods - ActiveRecord::Base.methods
=> ["field_types", "su_pw?", "set_login_attr", "create_user_and_conf_user",
"original_table_name", "field_type", "authenticate", "set_default_order",
"id_name?", "id_name_column", "original_locking_column", "default_order",
"subclass_associations", ...
# I ran the statements in the console.
사용자 클래스에 정의 된 (많은) has_many 관계의 결과로 생성 된 메서드 는 호출 결과에 없습니다methods
.
추가됨 : has_many는 메소드를 직접 추가하지 않습니다. 대신 ActiveRecord 기계는 Ruby method_missing
및 responds_to
기술을 사용하여 즉시 메서드 호출을 처리합니다. 결과적으로 방법은 방법 methods
에 대한 방법이 없습니다 .
공용 및 보호 된 인스턴스 메소드의 이름을 포함하는 배열을 리턴합니다. 모듈의 경우 공용 및 보호 된 메소드입니다. 클래스의 경우 인스턴스 (단일 항목이 아님) 메소드입니다. 인수가 인수가 반환되는 인수를 사용하면 mod의 메서드가 반환되고, 명명 된 메서드가 반환됩니다.
module A
def method1() end
end
class B
def method2() end
end
class C < B
def method3() end
end
A.instance_methods #=> [:method1]
B.instance_methods(false) #=> [:method2]
C.instance_methods(false) #=> [:method3]
C.instance_methods(true).length #=> 43
또는 User.methods(false)
유전자 클래스 유전자 정의 된 메서드 만 반환합니다.
넌 할 수있어
current_user.methods
더 나은 목록을 위해
puts "\n\current_user.methods : "+ current_user.methods.sort.join("\n").to_s+"\n\n"
이 중 하나는 어떻습니까?
object.methods.sort
Class.methods.sort
사용자 has_many 게시물이 가정합니다.
u = User.first
u.posts.methods
u.posts.methods - Object.methods
@clyfe의 대답을 설명합니다. 다음 코드를 사용하여 인스턴스 메소드 목록을 수 있습니다 ( "Parser"라는 개체 클래스가 존재 가정).
Parser.new.methods - Object.new.methods
인스턴스 (귀하의 경우 @current_user)에서 응답하는 메소드 목록을 찾고 있다면. 루비 문서화 방법에 따라
obj의 public 및 protected 메서드 이름 목록을 반환합니다. 여기에는 obj의 조상에서 액세스 할 수있는 모든 메서드가 포함됩니다. 선택적 매개 변수가 false이면 obj의 공용 및 보호 된 싱글 톤 메서드의 배열을 반환하며 배열은 obj에 포함 된 모듈의 메서드를 포함하지 않습니다.
@current_user.methods
@current_user.methods(false) #only public and protected singleton methods and also array will not include methods in modules included in @current_user class or parent of it.
또는 메서드가 개체에서 호출 가능한지 여부를 확인할 수도 있습니다.
@current_user.respond_to?:your_method_name
부모 클래스 메서드를 원하지 않으면 부모 클래스 메서드를 빼십시오.
@current_user.methods - @current_user.class.superclass.new.methods #methods that are available to @current_user instance.
참고 URL : https://stackoverflow.com/questions/8595184/how-to-list-all-methods-for-an-object-in-ruby
'IT' 카테고리의 다른 글
ASP.NET에서 Excel 파일 생성 (0) | 2020.08.20 |
---|---|
entitymanager가 어디에 있습니까? (0) | 2020.08.20 |
NUnit으로 개인 방법을 어떻게 테스트합니까? (0) | 2020.08.20 |
하나의 항목을 선택하는 Linq 코드 (0) | 2020.08.20 |
객체 키에 공백이 JavaScript 객체에 어떻게 액세스 할 수있는 있습니까? (0) | 2020.08.20 |