gpt4 book ai didi

Ruby,运行所有方法

转载 作者:太空宇宙 更新时间:2023-11-03 17:08:26 25 4
gpt4 key购买 nike

是否可以运行并输出所有的方法?不确定如何通过点运算符传递符号。所以不是 link.:node 它应该是 link.node

require 'mechanize'

agent = Mechanize.new
page = agent.get("http://stackoverflow.com/")


link = page.link
p meth = link.methods #=> [:node, :href, :attributes, :page, :referer, :click, :dom_id, :dom_class, :pretty_print, :inspect, :rel, :rel?, :noreferrer?, :text, :to_s, :uri, :pretty_print_cycle, :pretty_print_instance_variables, :pretty_print_inspect, :nil?, :===, :=~, :!~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup, :initialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?, :extend, :display, :method, :public_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :pretty_inspect, :==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]


#this doesn't work.
meth.each do |x|
puts "#{x}: #{link.x}"
end

最佳答案

如果想访问类的方法,可以使用以下方式

调用私有(private)方法

meth.each { |x| puts "#{x}: #{link.class.send(x)}" }

调用公共(public)方法

meth.each { |x| puts "#{x}: #{link.send(x)}" }

调用带参数的方法

meth.each { |x| puts "#{x}: #{link.class.send(x, params_or_arguments)}" }
meth.each { |x| puts "#{x}: #{link.send(x, params_or_arguments)}" }

关于Ruby,运行所有方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9419847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com