将哈希作为函数参数处理

发布于 2024-10-22 00:57:18 字数 434 浏览 1 评论 0 原文

我正在使用 Ruby on Rails 3,并且尝试将哈希作为函数参数进行处理。

例如,如果我这样声明一个函数:

def function_name(options = {})
  ...
end

我想将一个哈希值传递给 function_name

{"key1"=>"value_1", "key2"=>"value2", "..." => "..."}

,然后在函数内部使用它。

最好的\常见(Rails)方法是什么?

PS:我在某处见过 extract_option! 方法,但我不知道我不知道在哪里可以找到一些文档以及我是否需要这些文档来实现我的目标。

I am using Ruby on Rails 3 and I am trying to handle a hash as a function argument.

For example, if I state a function this way:

def function_name(options = {})
  ...
end

I would like to pass to the function_name a hash like

{"key1"=>"value_1", "key2"=>"value2", "..." => "..."}

and then use that inside the function.

What is the best\common (Rails) way to do that?

P.S.: I have seen the extract_option! method somewhere, but I don't know where I can find some documentation and whether I need that in order to accomplish what I aim.

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

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

发布评论

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

评论(3

淡忘如思 2024-10-29 00:57:18

只需使用您提供的定义:使用

def function_name(options = {})
  puts options["key1"]
end

以下方式调用它:

function_name "key1" => "value1", "key2" => "value2"

function_name({"key1" => "value1", "key2" => "value2"})

Array#extract_options! 只是与具有可变方法参数的方法一起使用,如下所示:

def function_name(*args)
  puts args.inspect
  options = args.extract_options!
  puts options["key1"]
  puts args.inspect
end

function_name "example", "second argument", "key1" => "value"
# prints
["example", "second argument", { "key1" => "value" }]
value
["example", "second argument"]

另一个有用的方法是 Hash#symbolize_keys! 让您不必关心是否向函数传递字符串或符号,以便您始终可以访问像这样的东西 options[:key1]

Simply use the definition you provided:

def function_name(options = {})
  puts options["key1"]
end

Call it with:

function_name "key1" => "value1", "key2" => "value2"

or

function_name({"key1" => "value1", "key2" => "value2"})

Array#extract_options! is simply used with methods that have variable method arguments like this:

def function_name(*args)
  puts args.inspect
  options = args.extract_options!
  puts options["key1"]
  puts args.inspect
end

function_name "example", "second argument", "key1" => "value"
# prints
["example", "second argument", { "key1" => "value" }]
value
["example", "second argument"]

Another useful method is Hash#symbolize_keys! which lets you not care about whether you pass in strings or symbols to your function so that you can always access things like this options[:key1].

南汐寒笙箫 2024-10-29 00:57:18

您在示例中声明它的方式可以正常工作。

def function(options = {})
    item = options[:item]
    need_milk = options[:milk] || false
    cow = options[:bovine]
end

function(:item => "Something")

在上面的例子中,item == "Something"need_milk == falsecow == nil

extract_options 只是通过 Rails 对 Array 和 Hash 类进行的补充。

def function(something, else, *args)
   options = args.extract_options! # returns Hash
end

如果您计划在 args 中包含许多不同类型的参数,那么它会很有用,但如果您只需要 Hash 选项,那么您原来的方式就可以了。

这是 Rails 中 extract_options! 代码的 Gist 我个人在我的代码在工作时只需将其写入外部文件并将其添加到我的项目中即可。

The way you have it declared in your example will work fine.

def function(options = {})
    item = options[:item]
    need_milk = options[:milk] || false
    cow = options[:bovine]
end

function(:item => "Something")

In the case above, item == "Something", need_milk == false and cow == nil.

extract_options is simply an addition to the Array and Hash class via Rails.

def function(something, else, *args)
   options = args.extract_options! # returns Hash
end

It is useful if you plan on having many different types of parameters in args but if you only want Hash options, your original way is fine.

Here's a Gist of the code in Rails for extract_options! I personally use it in my code at work by just writing it to an external file and requiring it into my project.

送你一个梦 2024-10-29 00:57:18

Ruby 让这一切变得简单,而且您已经做对了。

这是一个诗歌模式(最小,DSL 风格)的示例:

 def f x = {}
   p x
 end

 f :a => :b
 {:a=>:b}

 f
 {}

 f :a => :b, :c => :d
 {:a=>:b, :c=>:d}

Ruby makes this easy, and you were already doing it right.

Here is a poetry-mode (minimal, DSL-style) example:

 def f x = {}
   p x
 end

 f :a => :b
 {:a=>:b}

 f
 {}

 f :a => :b, :c => :d
 {:a=>:b, :c=>:d}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文