Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Allow Turnip::Table#hashes to be accessed by string or symbol keys #86

wants to merge 1 commit into


None yet
2 participants

The Problem

We've been wasting time trying to figure out why our tests are failing and it was due to us expecting the Table#hashes to return hashes with symbol keys (which is the convention) and we get nils.

First Attempt

I tried to fix this the first time around by mapping the strings to symbols in the Table#headers definition but you didn't like that because if someone had a header in their table like "Foo Bar" it wouldn't map well.

The Solution Now

I created a class called HashWithIndifferentAccess (I know there's a rails class called the same but this is not that) that sets the default_proc (which is run when you request from a hash instance a key a that it doesn't contain). This proc first checks to see if the key is a string or a symbol, then to see if there is a key which is of the other respective type in this hash. If there is then it returns that value, if not it returns nil, like usual.

How about that?


jnicklas commented Jan 26, 2014

Sorry for the late reply. Working through old issues and I found this.

Unfortunately this problem is pervasive in Ruby land, so keeping track of where symbols and where strings are used as keys kind of comes with the territory. You can't expect to do Ruby development and not get tripped up by this at a some point. Just another reason that symbols are a fundamentally stupid idea, left over from another time where the minor perf difference actually made a difference.

In this case using string keys makes sense, since column names are usually expected to be human readable, Gherkin is supposed to be a human readable language after all. In other words this:

Then some table
| Column Header |
| 1             |
| 2             | 

Should be preferred over this:

Then some table
| column_header |
| 1             |
| 2             |

A Symbol like :"Column Header", while allowed, is weird and unintuitive.

Including a HashWithIndifferentAccess class in Turnip is not sensible, imho.

@jnicklas jnicklas closed this Jan 26, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment