| Struct | Hash | OpenStruct | HashWithIndifferentAccess | [your custom class] | SimpleHash | |
|---|---|---|---|---|---|---|
new({ a: 1 }) / [a: 1] |
✗ | ✓ | ✓ | ✓ | ? | ✓ |
.name / .writing? |
✓ | ✗ | ✓ | ✗ | ? | ✓ |
[:name] |
✓ | ✓ | ✓ | ✓ | ? | ✓ |
[:name] / ["name"] |
✓ | ✗ | ✓ | ✓ | ? | ✓ |
.keys / .values / etc. |
✗ | ✓ | ✗ | ✓ | ? | ✓ |
NoMethodError |
✓ | ✓ | ✗ | ✓ | ? | ✓ |
| did you mean | ✓ | ✗ | ✗ | ✗ | ? | ✓ |
.to_json |
✓ | ✓ | ✗ | ✓ | ? | ✓ |
.emails.first.domain |
✗ | ✗ | ✗ | ✗ | ? | ✓ |
Last active
April 24, 2019 17:46
-
-
Save localhostdotdev/e6b5470b4e1a63394f8f30bb35b0d8ed to your computer and use it in GitHub Desktop.
testing different kind of key-value structures
This file contains hidden or 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
| $struct = Struct.new(:name, :writing?).new("localhostdotdev", true) | |
| $hash = Hash[name: "localhostdotdev", writing?: true] | |
| $openstruct = OpenStruct.new(name: "localhostdotdev", writing?: true) | |
| $hashwithindifferentaccess = HashWithIndifferentAccess[name: "localhostdotdev", writing?: true] | |
| $yourcustomclass = "<insert your class here>" | |
| $simplehash = SimpleHash[name: "localhostdotdev", writing?: true] | |
| def try(code) | |
| puts code | |
| p eval(code, binding) | |
| rescue Exception => e | |
| puts e.message | |
| end | |
| def banner(message) | |
| puts | |
| puts "=== #{message} ===" | |
| puts | |
| end | |
| banner ".name" | |
| try('$struct.name') | |
| try('$hash.name') | |
| try('$openstruct.name') | |
| try('$hashwithindifferentaccess.name') | |
| try('$simplehash.name') | |
| banner ".keys" | |
| try('$struct.keys') | |
| try('$hash.keys') | |
| try('$openstruct.keys') | |
| try('$hashwithindifferentaccess.keys') | |
| try('$simplehash.keys') | |
| banner "[:name]" | |
| try('$struct[:name]') | |
| try('$hash[:name]') | |
| try('$openstruct[:name]') | |
| try('$hashwithindifferentaccess[:name]') | |
| try('$simplehash[:name]') | |
| banner '["name"]' | |
| try('$struct["name"]') | |
| try('$hash["name"]') | |
| try('$openstruct["name"]') | |
| try('$hashwithindifferentaccess["name"]') | |
| try('$simplehash["name"]') | |
| banner ".names" | |
| try('$struct.names') | |
| try('$hash.names') | |
| try('$openstruct.names') | |
| try('$hashwithindifferentaccess.names') | |
| try('$simplehash.names') | |
| banner ".to_json" | |
| try('$struct.to_json') | |
| try('$hash.to_json') | |
| try('$openstruct.to_json') | |
| try('$hashwithindifferentaccess.to_json') | |
| try('$simplehash.to_json') |
This file contains hidden or 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
| === .name === | |
| $struct.name | |
| "localhostdotdev" | |
| $hash.name | |
| undefined method `name' for {:name=>"localhostdotdev", :writing?=>true}:Hash | |
| $openstruct.name | |
| "localhostdotdev" | |
| $hashwithindifferentaccess.name | |
| undefined method `name' for {"name"=>"localhostdotdev", "writing?"=>true}:ActiveSupport::HashWithIndifferentAccess | |
| $simplehash.name | |
| "localhostdotdev" | |
| === .keys === | |
| $struct.keys | |
| undefined method `keys' for #<struct name="localhostdotdev", :writing?=true> | |
| $hash.keys | |
| [:name, :writing?] | |
| $openstruct.keys | |
| nil | |
| $hashwithindifferentaccess.keys | |
| ["name", "writing?"] | |
| $simplehash.keys | |
| ["name", "writing?"] | |
| === [:name] === | |
| $struct[:name] | |
| "localhostdotdev" | |
| $hash[:name] | |
| "localhostdotdev" | |
| $openstruct[:name] | |
| "localhostdotdev" | |
| $hashwithindifferentaccess[:name] | |
| "localhostdotdev" | |
| $simplehash[:name] | |
| "localhostdotdev" | |
| === ["name"] === | |
| $struct["name"] | |
| "localhostdotdev" | |
| $hash["name"] | |
| nil | |
| $openstruct["name"] | |
| "localhostdotdev" | |
| $hashwithindifferentaccess["name"] | |
| "localhostdotdev" | |
| $simplehash["name"] | |
| "localhostdotdev" | |
| === .names === | |
| $struct.names | |
| undefined method `names' for #<struct name="localhostdotdev", :writing?=true> | |
| Did you mean? name | |
| name= | |
| $hash.names | |
| undefined method `names' for {:name=>"localhostdotdev", :writing?=>true}:Hash | |
| $openstruct.names | |
| nil | |
| $hashwithindifferentaccess.names | |
| undefined method `names' for {"name"=>"localhostdotdev", "writing?"=>true}:ActiveSupport::HashWithIndifferentAccess | |
| $simplehash.names | |
| undefined method `names' for {"name"=>"localhostdotdev", "writing?"=>true}:SimpleHash | |
| Did you mean? name | |
| === .to_json === | |
| $struct.to_json | |
| "{\"name\":\"localhostdotdev\",\"writing?\":true}" | |
| $hash.to_json | |
| "{\"name\":\"localhostdotdev\",\"writing?\":true}" | |
| $openstruct.to_json | |
| "{\"table\":{\"name\":\"localhostdotdev\",\"writing?\":true}}" | |
| $hashwithindifferentaccess.to_json | |
| "{\"name\":\"localhostdotdev\",\"writing?\":true}" | |
| $simplehash.to_json | |
| "{\"name\":\"localhostdotdev\",\"writing?\":true}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment