How can I return an anonymous object from a method in ruby.?
In the following code, I am returning an hash.
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
Any thoughts.?
Openstruct?
require 'ostruct'
def helper
OpenStruct.new(url: '', params: '')
end
Try this:
def helper
Class.new do
def url
''
end
def params
''
end
end.new
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With