Top hat, monocle and sweet stashA common gripe I’ve heard people bring up about Ruby is the fact that you can monkey-patch basically anything at any point during runtime. The result is that the world your code expects could potentially change under it and all hell breaks loose.  What if you included a 3rd party library that changed your expectations of .to_s?

Here’s a contrived example:

There is another way to monkey-patch that is safer — refinements!  Refinements have been in Ruby since 2.0 and essentially provides a way to namespace your monkey-patching efforts.

Creating and using refinements is pretty easy and requires:

  1. Create a module to hold your refinements
  2. USE that module inside your code

When making use of refinements the changes are scoped/limited within the scope of the module using said refinements. Here’s an example:

First we can create a module to hold our changes to the Integer class:

Next, via using, we can “use” this refinement in our own code!

Now finally here’s an example of calling CrazyInteger and how using refinements has protected Integer#to_s outside of the scope of CrazyInteger‘s class.

There’s an amazing quote I see floating around the Internet sometimes…

“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live”

The quote is sometimes mistakenly credited to Martin Golding in 1994.

The reality is that in 1991 John F. Woods authored it in a post to the comp.lang.c++ newsgroup.  The body of his message follows:

>Mark>I was wondering why it seems that the comma operator is so rarely used.
>Mark>The only time I ever see it is in ‘for’ loops. Is it really considered
>Mark>*that* bad by the programming public at large? Any comments?
>Rob>Well, I hadn’t seen it used much either outside of the for loop, but
>Rob>in Plaugher’s latest book I discovered quite a few of the following
>Rob>constructs:
>Rob> if (condition)
>Rob> var = value, anothervar = anothervalue;
>Rob>This does away with the need for braces. I am tempted to use this myself
>Rob>unless someone has a good point agains using this style. Opinions anyone?
>Consider this:
> if (condition)
> var = value; anothervar = anothervalue;
>Only one little dot is changed, but the meaning is quite different. In other
>words, using the comma operator like that makes it harder to read:
Right.

Always code as if the guy who ends up maintaining your code will be a
violent psychopath who knows where you live. Code for readability.

Amazingly, thanks to Google’s mirror of the comp.lang.c++ newsgroup, you can actually view John F. Woods’ post.

P.S. Just in case that post is removed or Google shuts down its mirror… here is a screenshot of the entire conversation.

descartes looking flyHere’s a cool chain of info nuggets that starts @ Renè Descartes and ends @ Ruby’s Array.product, lol.

Renè Descartes was a French philosopher and mathematician born in 1596.

He’s known for lots of things but one of the most popular is the following quote  from a passage in his publication Discourse on Method (1637):

I think, therefore I am” (or the corrected version “I am thinking, therefore I exist“)

Renè Descartes  developed “analytic geometry”, an algebraic description of geometry.  A pair of numbers can represent a specific coordinate on a plane or graph.  Sets of coordinates can be represented on a graph as well… and this gets us closer to the end of this nugget chain…

“Analytic geometry” is also known as “Cartesian geometry”… with the name stemming from Descartes’ own name.  In the Cartesian view, when given a plane (think … graph), any point on the plane can be represented by coordinates — in a 2 dimensional plane these might be X and Y, in a 3 dimensional space this might be something like X, Y and Z.

A cartesian product is the result of taking  two sets and returning all permutations of their combined results.  So for example, if you had a set of numbers named X that contained: “1,2,3” and a set of numbers called Y that contained “-5,-6,-7”, their cartesian product would be:

[1, -5], [1, -6], [1, -7], [2, -5], [2, -6], [2, -7], [3, -5], [3, -6], [3, -7]

And finally getting back to Ruby…. this is where Array.product gets it’s name!

Array.product is awesome and will get you the Cartesian product for a given array against a number of other arrays!

/fin

EVIL MASTERMIND... muhahahah aha AppAcademy had us coding some games today! Specifically, Mastermind and Hangman.

When my pair Brittney and I started this we had both never heard of mastermind; so that was the first challenge. Turns out mastermind is pretty straight forward AND it’s actually a really cool game.

Let me explain the rules of mastermind first…

You basically have a board that allows for 10 turns. There’s a code-maker and a code-breaker. The board has 4 columns and a row for each of the 10 turns. Each row (or turn) also includes a “correctness area” (just baked that term up fresh) for the code-maker to indicate how many of the 4 were wrong, near or correct; I’ll explain those soon. For our purposes the computer was the code-maker.

The code-maker’s role is to make a secret “code” that consists of 4 colors selected from a list of 6 available options. Once the code-maker has selected a code he keeps it a secret and allows the code-breaker to guess it.

As I mentioned above, the code-breaker gets 10 attempts to crack the code. Each time he makes a guess he sets 4 colors from the 6 available on the row corresponding to his turn. At that point, the code-maker takes a look at the code-breaker’s choices and using the “correctness area” indicates if any of his/her choices were exact, near or wrong.

If a choice was exact, meaning the color and column selected were correct, a black marker is placed in the “correctness area”.
If a choice was near, meaning the code-breaker selected a correct color but didn’t place it in the correct spot, a grey dot is placed in the “correctness area”.
If a choice is flat in every sense, meaning it’s a color not used in the code, then we do nothing.

If the code-breaker guesses the code before the 10 turns are over, he wins! woot!

Our implementation basically had 3 classes a Computer (the code-maker), a Player (code breaker) and a Game class that actually “played” the game.

The full source is as follows:

 

pairon

Under construction still…

Today was Awesome.  In my past experiences I’ve found that pairs balance out as they work but with Kriti I think we balanced out as a whole.  To explain that a bit further… Kriti is incredibly strong with recursion problems … like straight up a bad ass (and I have a line of code to show for it) and I’m stronger with some of the more common Ruby idioms — test first and similar stuff in that vein.  Instad of balancing out on a per problem basis it felt like we kind of showed our strengths against problems we were better at and the result was a good balance across entire duration of working together.

redneck-recursion

 

We worked on a lot of recursive problems and while working on one in particular ended up making a pretty ridiculous mistake… We used #each instead of #map. After probably an hour of trying to figure out why our ___________ method wouldn’t work we realized that we had been using #each (probably out of habit since it’s so common) and our results were coming out totally unchanged.

We actually ended up writing the same method like 3 different ways trying to resolve the issue and FINALLY zeroed in the fact that nothing was changing -> #each iterates and doesn’t make changes -> #map iterates and DOES make changes -> OMFGWTF

In other news, here’s an awesome, awesome, method that Kriti came up with.

____________________f

giggityHere’s a fun snippet of code we wrote today at AppAcademy.

Basically the goal was to take a bunch of couples who were into swinging and swap all of their partners.  Yep, swinging.. as in:

Swinging or (rarely) partner swapping is a non-monogamous behavior, in which singles or partners in a committed relationship engage in sexual activities with others as a recreational or social activity.[1] Swinging can take place in a number of contexts, ranging from spontaneous sexual activity at informal gatherings of friends to planned regular social meetings to hooking up with like-minded people at a swingers’ club. It can also involve Internet-based swinger social networking services online.
Wikipedia

In order to properly swing, a couple is supposed to show up at a party and swap their partner for another partner.  For our purposes we were to assume couples were heterosexual males and females.

The code we came up with was this:

Basically we found that we could randomize all of the couples then shift their partners by one.  By doing it this way we could ensure things that both things were spicy AND that a man never ended up with the same woman he arrived with.

Ned Ruggeri - AppAcademy teacher

Today marks the first day of my sprint through San Francisco based AppAcademy. It’s a pretty interesting setup.  Basically, you’re paired with a random person everyday and work through a large suite of problems assigned for that particular day.

To say the least, you and your pair bust ass HARD until the end of the day.  The day ends with more work — you are now assigned another pair and must review their code.  After reviewing the other pair’s code and sending them some comments the final part of the day (now night) is doing the readings for the next day’s schedule.

It’s very intense but the amount of code we are producing is really fantastic.

The teachers and TAs are VERY approachable.  Ned Ruggeri (mug shot on the right) presents complex concepts in great detail and definitely seems to genuinely care if people are “getting” things or not.

It’s a great environment.