> s.methods.map {|n| n if n =~ /time/} => [nil, nil, nil, nil, nil, nil, nil, nil, "-6ren">
gpt4 book ai didi

ruby - 在Ruby中,过滤一个对象中所有包含单词 "time"的方法的好方法是什么?

转载 作者:数据小太阳 更新时间:2023-10-29 07:40:31 39 4
gpt4 key购买 nike

我尝试了以下方法,部分有效:

>> s.methods.map {|n| n if n =~ /time/}
=> [nil, nil, nil, nil, nil, nil, nil, nil, "skip_time_zone_conversion_for_attri
butes", nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, ni
l, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, ni
l, "timestamped_migrations", nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, n
il, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, n
il, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, n
il, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, n
il, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, "time_zone_aware
_attributes", nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, n
il, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, "default_timezone", nil, n
il, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, n
il, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, n
il, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, n
il, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, "recor
d_timestamps", nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil, nil]

>> s.methods.each {|n| p n if n =~ /time/}
"skip_time_zone_conversion_for_attributes"
"timestamped_migrations"
"time_zone_aware_attributes"
"default_timezone"
"record_timestamps"
=> ["extended_by", "before_create", "vote_ids=", "save_without_dirty", "_delete"
, "touch", "daemonize", "after_destroy", "skip_time_zone_conversion_for_attribut
es", "methods", "send", "to_query", "becomes", "after_validation", "store_full_s
ti_class?", "save_with_transactions!", "autosave_associated_records_for_votes",
"require_library_or_gem", "enum_for", "taint", "instance_variable_defined?", "ac
[...] and the rest of the whole array

>> s.methods.filter {|n| n =~ /time/}
NoMethodError: undefined method `filter' for #<Array:0x4de6b00>
from (irb):93

最佳答案

grep是实现此目的的另一种简单方法:

1.9.0 > require 'date'
=> true
1.9.0 > x = Date.new
=> #<Date: -1/2,0,2299161>
1.9.0 > x.methods.grep /time/
=> ["ctime", "asctime", "strftime"]

使用您尝试过的方法:

map 将给定的 block 应用于给定可枚举的每个元素,返回一个新的可枚举。这不是您想要的(如您所见)。

methods.each 有点用处,但显然简单地打印出结果并不是很有用。它根本不像 Ruby,但您可以这样做:

matching_methods = []
s.methods.each {|m| matching_methods << m if m =~ /time/}

将匹配/time/的每个方法累加到matching_methods数组中。当然,如果你这样做,那么

s.methods.select { |m| m =~ /time/ }

更可取。

最后,filter 在 Ruby 中不存在;这就是 select(或 find_all)的作用。

关于ruby - 在Ruby中,过滤一个对象中所有包含单词 "time"的方法的好方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2894589/

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