Last active
September 22, 2022 15:30
-
-
Save thadeu/1fe7b8633eb0b8b081baa1de5c210483 to your computer and use it in GitHub Desktop.
Ruby Deep OpenStruct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'json', require: true | |
end | |
class Hash | |
def to_o | |
JSON.parse to_json, object_class: OpenStruct | |
end | |
end | |
response = { | |
body: { | |
status: 200, | |
errors: ['name is required'], | |
success: true, | |
data: { name: 'Thadeu' } | |
} | |
}.to_o | |
# before | |
p response['body']['errors'] | |
# after | |
p response.body.success | |
p response.body.errors | |
p response.dig(:body, :data, :name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment