Skip to content

Instantly share code, notes, and snippets.

@worace
Created March 31, 2015 23:34
Show Gist options
  • Save worace/561fba1b5eef00652b11 to your computer and use it in GitHub Desktop.
Save worace/561fba1b5eef00652b11 to your computer and use it in GitHub Desktop.
Turing Struct Warmup

Warmup - The Turing Struct

Let's create a new Turing Struct class which wraps a hash and exposes its keys as methods.

A Turing Struct accepts a hash when it's initialized, e.g.:

TuringStruct.new({my: "hash"})

When we provide a hash to a TuringStruct, we can then ask it for the keys of that hash as if they were methods:

struct = TuringStruct.new({some: "value", another: "thing"})
struct.some
# => "value"
struct.another
# => "thing"

Extension: Once you have a working struct, see what it would take to allow method-chaining to retrieve nested hash data

struct = TuringStruct.new({nested: {another: "hash"})
struct.nested.another
# => "hash"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment