ste*_*ard 9 ruby methods inheritance
我想创建一个具有一个方法的类,该方法调用不在超类中的所有其他方法.
有没有办法我可以使用obj.methods来获取非祖先方法?或者还有另一种方法可以完全实现.
谢谢
Sim*_*tsa 14
该标准instance_methods允许您指定是否要包含超类方法:
class Foo
def bar
end
end
Foo.instance_methods(false) # => [:bar]
Run Code Online (Sandbox Code Playgroud)
gle*_*ald 10
我不确定你在这里真正要做什么,也不确定你用"all"指的是哪种方法,但如果问题是你如何弄清楚哪个类的实例方法没有被继承,那么.instance_methods的组合和.ancestors可以得到你的信息.在这里,使用Array作为示例类:
Array.instance_methods.sort
=> ["&", "*", "+", "-", "<<", "<=>", "==", "===", "=~", "[]", "[]=", "__id__", "__send__", "all?", "any?", "assoc", "at", "class", "clear", "clone", "collect", "collect!", "compact", "compact!", "concat", "delete", "delete_at", "delete_if", "detect", "display", "dup", "each", "each_index", "each_with_index", "empty?", "entries", "eql?", "equal?", "extend", "fetch", "fill", "find", "find_all", "first", "flatten", "flatten!", "freeze", "frozen?", "grep", "hash", "id", "include?", "index", "indexes", "indices", "inject", "insert", "inspect", "instance_eval", "instance_of?", "instance_variable_get", "instance_variable_set", "instance_variables", "is_a?", "join", "kind_of?", "last", "length", "map", "map!", "max", "member?", "method", "methods", "min", "nil?", "nitems", "object_id", "pack", "partition", "pop", "private_methods", "protected_methods", "public_methods", "push", "rassoc", "reject", "reject!", "replace", "respond_to?", "reverse", "reverse!", "reverse_each", "rindex", "select", "send", "shift", "singleton_methods", "size", "slice", "slice!", "sort", "sort!", "sort_by", "taint", "tainted?", "to_a", "to_ary", "to_s", "transpose", "type", "uniq", "uniq!", "unshift", "untaint", "values_at", "zip", "|"]
Array.ancestors
=> [Array, Enumerable, Object, Kernel]
Array.instance_methods.sort - Array.ancestors.map {|a| a == Array ? [] : a.instance_methods}.flatten
=> ["&", "*", "+", "-", "<<", "<=>", "[]", "[]=", "assoc", "at", "clear", "collect!", "compact", "compact!", "concat", "delete", "delete_at", "delete_if", "each", "each_index", "empty?", "fetch", "fill", "first", "flatten", "flatten!", "index", "indexes", "indices", "insert", "join", "last", "length", "map!", "nitems", "pack", "pop", "push", "rassoc", "reject!", "replace", "reverse", "reverse!", "reverse_each", "rindex", "shift", "size", "slice", "slice!", "sort!", "to_ary", "transpose", "uniq", "uniq!", "unshift", "values_at", "|"]
Run Code Online (Sandbox Code Playgroud)
如果你真的只想排除超类中的方法,而不是包含的方法,那么还有.superclass.
Array.superclass
=> Object
Array.instance_methods.sort - Array.superclass.instance_methods
=> ["&", "*", "+", "-", "<<", "<=>", "[]", "[]=", "all?", "any?", "assoc", "at", "clear", "collect", "collect!", "compact", "compact!", "concat", "delete", "delete_at", "delete_if", "detect", "each", "each_index", "each_with_index", "empty?", "entries", "fetch", "fill", "find", "find_all", "first", "flatten", "flatten!", "grep", "include?", "index", "indexes", "indices", "inject", "insert", "join", "last", "length", "map", "map!", "max", "member?", "min", "nitems", "pack", "partition", "pop", "push", "rassoc", "reject", "reject!", "replace", "reverse", "reverse!", "reverse_each", "rindex", "select", "shift", "size", "slice", "slice!", "sort", "sort!", "sort_by", "to_ary", "transpose", "uniq", "uniq!", "unshift", "values_at", "zip", "|"]
Run Code Online (Sandbox Code Playgroud)
这有帮助吗?
| 归档时间: |
|
| 查看次数: |
2583 次 |
| 最近记录: |