从Ruby中的方法返回匿名对象

Muthukumar

如何从ruby中的方法返回匿名对象?

在以下代码中,我将返回一个哈希。

  def process
    source = helper

    # I am able to get the value as an hash
    puts source[:url]
    puts source[:params]

    # But I wonder if there is a way to get it as an object, so that I can use the dot notation
    # puts source.url
    # puts source.params
  end

  def helper
    url = ''
    params = ''
    return {url: url, params: params}
  end

有什么想法吗。?

熊类

Openstruct的

require 'ostruct'

def helper
  OpenStruct.new(url: '', params: '')    
end

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章