【发布时间】:2023-03-16 14:47:01
【问题描述】:
是否可以运行并输出所有方法?不确定如何通过点运算符传递符号。所以应该是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
【问题讨论】:
-
其中许多方法都需要参数,没有它们就无法调用。你想完成什么?
-
你在什么上运行这些方法?此外,现在它正在输出一个字符串,其中仅包含方法名称,后跟不带参数传递的方法。
-
帮助我学习。方法是符号,不知道如何将符号推入点运算符....所以不要传递
link.:node,应该是link.node