ok, this is very cool the line you're look at starts with the variable 'sections', which is defined previously sections is a data type called a hash ; it associates values, like two columns in a spreadsheet - each row forms a pair Monday: 3 Tuesday: 12 Wednesday: 99 etc so
-
-
Replying to @MorlockP @NoLongerBennett
we then say http://sections.select select is a method that gets invoked on sections, and at a high level is says "inspect every element of this, and winnow down to a subset that matches certain conditions" the conditions are specified inside the squiggle brackets { ... }
1 reply 0 retweets 0 likes -
Replying to @MorlockP @NoLongerBennett
the code inside those brackets is EFFECTIVELY a miniature function. It takes as input each "row" (in the spreadsheet analogy), and it evaluates it to a boolean - should this "row" be selected ? the rows in the spreadsheet analogy each have two elements in them we can call >>>
1 reply 0 retweets 0 likes -
Replying to @MorlockP @NoLongerBennett
the day of the week the "key" and we can call the number the "value". These are in fact the standard terms for the two parts of a "hash", the data type of sections. "Key" must be unique across the entire hash, but "value" can be repeated. Monday: 12 Tuesday: 12 but we can't >
1 reply 0 retweets 0 likes -
Replying to @MorlockP @NoLongerBennett
have "Monday" in there twice. So, back to the mini function. The syntax { |k, v| ... } says "define a mini function which takes two arguments on each invocation; first one is the key, second one is the value" so http://sections.select { | k, v| <minifunc> } ...
1 reply 0 retweets 0 likes -
Replying to @MorlockP @NoLongerBennett
* passes in k = Monday, v = 12 * evaluates the minifunc * gets either boolean true or false as a result * based on that, chooses or does not choose the pair k/v to put in the output bad_sections = http://sections.select { |k, v| k.count(":") + 1 != v } sets bad_sections
1 reply 0 retweets 0 likes -
Replying to @MorlockP @NoLongerBennett
to have only those items from the original variable sections that have an improper count(":")
0 replies 0 retweets 0 likes -
This Tweet is unavailable.
-
Replying to @NoLongerBennett
k is the key v is the value in a phone book, names are keys, phone numbers are values by convention, the order of items passed in is always key first, then value { |k, v| ... } I can name these variable whatever I want { |day-of-week, car-sales| ... }
0 replies 0 retweets 0 likes -
This Tweet is unavailable.
exactly let's rewrite my code using better variable names # sections is a hash ; each entry is # key = human readable section text # e.g. "Fuel: Diesel" # value = integer num of stars bad_sections = http://sections.select { |key, value| key.count(":") + 1 != value }
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.