Ruby 数学运算符可以存储在哈希中并在以后动态应用吗?

发布于 2024-12-20 07:31:26 字数 657 浏览 1 评论 0原文

有什么方法可以设置诸如 <>%+ 等值的哈希值ETC? 我想创建一个接受整数数组和带参数的哈希的方法。

下面的方法中,array是要过滤的数组,hash是参数。这个想法是删除任何小于 min 或大于 max 的数字。

def range_filter(array, hash) 
  checker={min=> <, ,max => >} # this is NOT  working code, this the line I am curious about
  checker.each_key {|key| array.delete_if {|x| x checker[key] args[key] }
  array.each{|num| puts num}
end

期望的结果是

array=[1, 25, 15, 7, 50]
filter={min=> 10, max=> 30} 
range_filter(array, filter)
# => 25
# => 15

Is there any way to set a hash with values such as <, >, %, +, etc?
I want to create a method that accepts an array of ints, and a hash with parameters.

In the method below array is the array to be filtered, and hash is the parameters. The idea is that any number less than min or more than max is removed.

def range_filter(array, hash) 
  checker={min=> <, ,max => >} # this is NOT  working code, this the line I am curious about
  checker.each_key {|key| array.delete_if {|x| x checker[key] args[key] }
  array.each{|num| puts num}
end

The desired results would be

array=[1, 25, 15, 7, 50]
filter={min=> 10, max=> 30} 
range_filter(array, filter)
# => 25
# => 15

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

埋葬我深情 2024-12-27 07:31:26

在 Ruby 中,甚至数学也是方法调用。数学符号可以存储为 ruby​​ 符号。这些行是相同的:

1 + 2         # 3
1.+(2)        # 3
1.send(:+, 2) # 3

因此,有了这些,将其存储为散列就很简单了:

op = { :method => :> }
puts 1.send(op[:method], 2) # false
puts 3.send(op[:method], 2) # true

In ruby, even math is a method invocation. And math symbols can be stored as ruby symbols. These lines are identical:

1 + 2         # 3
1.+(2)        # 3
1.send(:+, 2) # 3

So armed with that, storing it as a hash is simple:

op = { :method => :> }
puts 1.send(op[:method], 2) # false
puts 3.send(op[:method], 2) # true
月棠 2024-12-27 07:31:26

当然,将它们存储为字符串(或符号)并使用 object.send(function_name, argument)

> operators = ["<", ">", "%", "+"]
=> ["<", ">", "%", "+"] 
> operators.each{|op| puts ["   10 #{op} 3: ", 10.send(op,3)].join}
   10 < 3: false
   10 > 3: true
   10 % 3: 1
   10 + 3: 13

sure, store them as strings (or symbols) and use object.send(function_name, argument)

> operators = ["<", ">", "%", "+"]
=> ["<", ">", "%", "+"] 
> operators.each{|op| puts ["   10 #{op} 3: ", 10.send(op,3)].join}
   10 < 3: false
   10 > 3: true
   10 % 3: 1
   10 + 3: 13
岁月如刀 2024-12-27 07:31:26

这应该像预期的那样工作:

def range_filter(array, args)
  checker = { :min=> :<, :max => :> }
  checker.each_key { |key| array.delete_if {|x| x.send checker[key], args[key] } }
  array.each { |num| puts num }
end

只需使用 Symbol 而不是普通运算符。这些运算符是数字对象的特殊方法,因此您只需使用 send 及其等效的 Symbol 即可动态调用它们。

This should work just like expected:

def range_filter(array, args)
  checker = { :min=> :<, :max => :> }
  checker.each_key { |key| array.delete_if {|x| x.send checker[key], args[key] } }
  array.each { |num| puts num }
end

Just use Symbols instead of the plain operators. The operators are special methods of number objects so you can just use send and their Symbol equivalent to call them dynamically.

撩发小公举 2024-12-27 07:31:26

在这种情况下,猜测使用符号不会增加可读性。试试这个:

checkers = 
  [ lambda{ |x| x > 10 },
    lambda{ |x| x < 30 } ]

[1, 25, 15, 7, 50].select{ |x| checkers.all?{ |c| c[x] } } 
#=> [25, 15]

更新

比较(它也有效,但如果你想要 lambda{ |x| x % 3 == 1 } 呢?)

checkers = 
  { :> => 10,
    :< => 30 }

[1, 25, 15, 7, 50].select{ |x| checkers.all?{ |k, v| x.send(k, v) } } 
#=> [25, 15]

Guess using symbols doesn't add readability in this case. Try this:

checkers = 
  [ lambda{ |x| x > 10 },
    lambda{ |x| x < 30 } ]

[1, 25, 15, 7, 50].select{ |x| checkers.all?{ |c| c[x] } } 
#=> [25, 15]

Update

Compare with (it works too, but what if you wanted lambda{ |x| x % 3 == 1 } ?)

checkers = 
  { :> => 10,
    :< => 30 }

[1, 25, 15, 7, 50].select{ |x| checkers.all?{ |k, v| x.send(k, v) } } 
#=> [25, 15]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文