2016-10-21 4 views
-1

ルビのメソッドから匿名オブジェクトを返すにはどうしたらいいですか?ルビのメソッドから匿名オブジェクトを返します

次のコードでは、ハッシュを返しています。

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 

任意の考え。

+0

あなたの質問は不明です。 Rubyのオブジェクトには名前はなく、常に匿名です。あなたのコードでは、匿名のオブジェクト、つまり 'ハッシュ'を返しています。 –

答えて

4

Openstruct

require 'ostruct' 

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

ありがとうございます!これは私が探していたものです。 – Muthukumar

関連する問題