, @Shel
, , , , .
:
, .
OpenStruct , .
# This method is kind of factory to create OpenStruct instances
def get_recursive_ostruct(object)
if object.is_a?(Hash)
object = object.clone
object.each do |key, value|
object[key] = get_recursive_ostruct(value)
end
OpenStruct.new(object)
else
object
end
end
require 'ostruct'
hash = {first: 'this is first', second: {first: 'first of second', second: 'second of second'}}
obj = get_recursive_ostruct(hash)
obj.second.second
obj.second.third
, , , .
TXT.ftp.lastname ## raises NoMethodError
TXT.ftp.try(:lastname) ## returns nil if `lastname` is not available
TXT.try(:ftp).try(:lastname) ## returns nil even if `ftp` is not available
:
, try
Rails
IRB
ruby
.
: rescue
: ; , -. ,
TXT.ftp.lastname rescue nil
TXT.ftp.lastname rescue ''