创建一个接受参数散列的 ruby​​ 方法 [英] Create a ruby method that accepts a hash of parameters

查看:35
本文介绍了创建一个接受参数散列的 ruby​​ 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能是一个愚蠢的问题,但我不知道如何创建一个接受参数散列的 ruby​​ 方法.我的意思是,在 Rails 中,我想使用这样的方法:

I know this may be a stupid question, but I don't know how to create a ruby method that accepts a hash of parameters. I mean, in Rails I'd like to use a method like this:

login_success :msg => "Success!", :gotourl => user_url

接受这种参数的方法的原型是什么?我如何阅读它们?

What is the prototype of a method that accepts this kind of parameters? How do I read them?

推荐答案

如果您以散列语法将参数传递给 Ruby 函数,Ruby 会假定这是您的目标.因此:

If you pass paramaters to a Ruby function in hash syntax, Ruby will assume that is your goal. Thus:

def login_success(hsh = {})
  puts hsh[:msg]
end

这篇关于创建一个接受参数散列的 ruby​​ 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆