简体   繁体   English

如何在 ruby​​ 中编写一个采用散列样式、方括号括起来的参数的方法,比如 mymethod[arg]?

[英]How to write a method in ruby that takes hash-style, square bracketed arguments, like mymethod[arg]?

I want to write a method in ruby to replace a current method that simply returns a hash.我想用 ruby​​ 编写一个方法来替换当前只返回散列的方法。

So currently we have this所以目前我们有这个

def some_stats
  {
    :foo => "bar",
    :zoo => "zar"
  }
end

which can be called like this:可以这样调用:

some_stats[:foo]

I want to replace this with a method that is called with :foo, which does some more complicated things that just building a hash.我想用一个用 :foo 调用的方法来替换它,它会做一些更复杂的事情,只是构建一个散列。 However, I'd like it to be called in the same way, with the square bracket notation.但是,我希望以相同的方式调用它,并使用方括号表示法。 EG例如

#this is a bit pseudo-cody but hopefully you get the idea.
def some_stats[](key)
  if key.to_s =~ /something/
    #do something
    return something
  else
    #do something else
    return something_else
  end
end

This would still be called like some_stats[:foo]这仍然会像some_stats[:foo]一样被调用

I can't work out the syntax for defining the method name: the above, def some_stats[](key) doesn't work.我无法计算出定义方法名称的语法:上面的def some_stats[](key)不起作用。 I feel like this should be possible though.不过我觉得这应该是可能的。

You're quite close.你很接近。 object[sth] is just a syntax sugar for object.[](sth) . object[sth]只是object.[](sth)的语法糖。 So to do what you need you have to define some_stats method that returns the object which defines [] method:因此,要执行您需要的操作,您必须定义some_stats方法,该方法返回定义[]方法的对象:

class Stats
  def [](key)
    if key.to_s =~ /something/
     #do something
     return something
  else
    #do something else
    return something_else
  end
end

def some_stats
  Stats.new
end

some_stats[:something_new] #=> something
some_stats[:not_new] #=> something_else

Your original method is not called with square brackets ;您的原始方法不是用方括号调用的 it is called without any argument , but returns an object (in your case a Hash ), which has method :[] defined and hence understands square brackets.它在没有任何参数的情况下被调用,但返回一个对象(在你的情况下是一个Hash ),它定义了方法:[] ,因此理解方括号。 Your method call is equivalent to您的方法调用相当于

(some_stats())[:foo]

or, more explicitly, to或者,更明确地说,

x = some_stats()
x[:foo]

Therefore you need - inside your method return an instance of some class which offers the [] method.因此你需要 - 在你的方法中返回一个提供[]方法的类的实例。 Say you have written a class named MyClass which has a [] defined.假设您编写了一个名为MyClass的类,它定义了一个[] You could then write a function然后你可以写一个函数

def some_stats
  if _your condition goes here_
    { ... } # Return your Hash
  else
    Myclass.new
  end
end

This would be used as这将用作

some_stats[:whatever]

However, to be really useful, your condition would make use of a parameter to the method, ie但是,要真正有用,您的条件将使用该方法的参数,即

def some_stats(param)
  if param < 4000
    { ... } # Return your Hash
  else
    Myclass.new
  end
end

And you would invoke it by你会调用它

some_stats(4711)[:whatever]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM