我正在查看对象的所有方法。我该如何避免所有的问题?
r=1..10
r.methods.map{|m| puts m}
===
eql?
hash
each
step
bsearch
begin
end
first
...
instance_exec
__send__
__id__
=> [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
...
nil, nil, nil, nil, nil, nil, nil, nil, nil]
2.0.0-p247 :004 >
答案 0 :(得分:3)
使用map
执行的操作会将结果返回到另一个数组中.. puts
会返回nil
..而irb
会打印返回值。所以这是预期的行为。
您可以使用puts r.methods
或irb
r.methods
irb(main):002:0> (1..2).methods
=> [:==, :===, :eql?, :hash, :each, :step, :begin, :end, :first, :last, :min, :max, :to_s, :inspect, :exclude_end?, :member?, :include?, :cover?, :to_a, :entries, :sort, :sort_by, :grep, :count, :find, :detect, :find_index, :find_all, :select, :reject, :collect, :map, :flat_map, :collect_concat, :inject, :reduce, :partition, :group_by, :all?, :any?, :one?, :none?, :minmax, :min_by, :max_by, :minmax_by, :each_with_index, :reverse_each, :each_entry, :each_slice, :each_cons, :each_with_object, :zip, :take, :take_while, :drop, :drop_while, :cycle, :chunk, :slice_before, :nil?, :=~, :!~, :<=>, :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, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]
如果你想让他们各自独立,你也可以.join "\n"
他们
另外,为了清楚起见,该数组是表达式的返回值,默认情况下显示在irb
中。如果您像在文件中一样编写代码,它将被忽略。在这种情况下不会打印出来。
答案 1 :(得分:2)
使用Array#each
(1..10).methods.each {|m| puts m}