For some reason my brain has defected and has chosen a life of NEVER being able to recall basically anything related to Ruby’s % notation. After seeing this list on a recent Arkency blog post (an awesome blog) I’ve decided to steal repost it here for my own personal reference.


 

%q[ ] # Non-interpolated String (except for \\ \[ and \])
%Q[ ] # Interpolated String (default)
%r[ ] # Interpolated Regexp (flags can appear after the closing delimiter)
%i[ ] # Non-interpolated Array of symbols, separated by whitespace
%I[ ] # Interpolated Array of symbols, separated by whitespace
%w[ ] # Non-interpolated Array of words, separated by whitespace
%W[ ] # Interpolated Array of words, separated by whitespace
%x[ ] # Interpolated shell command

ice_cube_gangster_fishing_at_first_i_was_likeHave you ever had this happen to you?

  1. You finish writing some awesome code.  You’re a bad ass and you know it.
  2. You hit the command line, because you stopped creating GUI interfaces using visual basic a long time ago.
  3. Issue a little “git add” here and there, add some broth, a potato.  Baby, you’ve got a stew going… So you commit!
  4. You do the habitual post-commit “git status” for no good reason and there it is, the file you friggen forgot to add to the commit you already committed to.

Fortunately git has a really convenient way to amend to your last commit!  Just simply stage the file(s) you missed and run:

git commit --amend

wat-gigantic-duckFirst off, I stole this pic from Greg Pike’s post about Javascript.  Now that the confession is out of the way…

Today at App Academy my pair and I were asked to explain the meaning of Ruby’s “double pipe equals” operator  to our cohorts.  I figured that might be a good topic to post here.

The double pipe equals, which will be referred to as “||=” from now on, is an operator that’s used for conditional assignment.

In a nutshell the||= operator’s usage is something like this:

blah ||= blabbity_blah()

This essentially says: “if blah is false then set it to blabbity_blah(), otherwise leave it set to whatever it was before we started”

Here’s a contrived, but-still-real-code, example lifted (and modified) from Wikibooks:

 

im here to serve and protect and get my treatsRuby provides 3 ways of classifying a methods in a Class: Public, Private and Protected.  These keywords are used to declare a method’s visibility and essentially what you’re doing is restricting access to the individual methods contained in your class.

Here’s a quick and dirty about the differences:

Public

These methods are accessible by anyone who calls your class.  They define your class’s public “interface”.

Protected

Protected methods are accessible by objects of the same class.  By this definition, a good use case is when classes extend your class and want to access those methods.  The subclass will retain access into the super class’s protected methods.

Private

Private methods can mask the gory details of how the public/protected take input and generate output.  One way to look at this is that these methods are essentially for the “author of the object/class”.  And by “author” I do mean the guy who wrote the code.  Because private methods are not publicly exposed — aka someone who calls your class doesn’t have access to them — private can be considered free for refactoring, changing and deletion without worrying that your outside callers are going to have problems.

Here’s a cool trick for dealing with situations where you want to assign multiple values at the same time.

The technique used is called parallel assignment and it looks something like this:

 

 

If you’re working with game boards, this is super useful way to pass around board coordinates!!

infinite-loopIf anyone actually reads this, here’s an unusual circumstance to look out for… circular object references that cause Pry to go BSC.

(For those who don’t know, Pry is a Ruby shell that can be used as a replacement for the default Ruby shell, IRB (like IPython for python people).)

Today I witnessed a pretty strange phenomenon where I had a game board that contained a bunch of objects representing items on the board.  Each item involved in the game also contacted a reference back to the game board.  After building this circular awareness we starting having problems with Pry where it would just flat out lock up and tax the CPU like crazy.

After some investigation we narrowed things down to the inspect method and the fact that whenever a statement in pry is executed (and irb too… I think) the inspect method is called on it.  This ultimately gives you the nice visual representation of your objects we all love.

After some investigation and soliciting help from peers it looks like this was probably caused by the fact that the game board object referenced other game objects that in turn referenced the game board…. and so on…

In the past, this has NEVER been a problem for me, and later in the day I wasn’t about to reproduce it with different code… the situation today definitely caused a problem and wasted at least 30 minutes.  🙁

spike_and_rarity__s_heart_shaped_fire_ruby_by_edwardtenDid you know it’s possible to run (aka build) your Ruby code straight from inside of Sublime Text 2?

That’s easy, just make sure your code is saved as a .rb file and hit Command+B.  But wait, there’s more…

Did you also know it’s possible to run your Ruby code straight from inside of Sublime Text 2… using RVM’s default Ruby?

There might be a better way but here’s one a quick and dirty for an OSX environment.  If you are using Linux, it’s hopefully the same but with the file paths modified:

Step 1:
Cut a hole in a box.

Step 2:
Open `~/Library/Application Support/Sublime Text 2/Packages/Ruby/Ruby.sublime-build`
And replace it’s contents with the following (be sure to substitute your user name):

 

Step 3:

  • Launch Sublime Text 2
  • Open a ruby file
  • Hit Command+B
  • The Sublime Text console should pop up with your output. If you want it to go away, hit escape.

Want to prove it’s working?

Here’s a quick script that computes factorial and then prints out the result AND the ruby version using ruby’s constant “RUBY_VERSION”:

 

Drop that into your own factorial.rb, open it in Sublime Text, and hit Command+B.  It should look something like this:

sublime-text-doing-the-most

As you can see above, that’s 1.9.3… and as you can see below… that’s what my RVM is set to use as it’s default ruby.

 

Some Preface

An app I’m working on has a rake that that basically gathers data from the web.  Doing things serially is really REALLY slow and to get around this I started using the (fantastic) parallel gem by Michael Grosser.

This gem is great — it’s simple to use and in something like 3 minutes after installing it I had my task running in 10 processes and absolutely murdering the work that needed to be done.

The Issue

I swapped databases not too long ago and moved from MySQL to PostgreSQL for full text search (which i don’t use anymore) and to stay in line with my host, Heroku.

Today i attempted to run the task against my PostgreSQL db and came up with an error I had never seen before:

message type 0x5a arrived from server while idle

WHUT?

After some investigating on Google I found the root cause — PostgreSQL does not allow using the same connection for more than one thread.

That’s pretty straight forward.

I believe the issue was that I had something like 10 ruby processes that were spawned from 1 process that was holding the db connection.  Not allowed!

The Solution

The solution is actually very straight forward as well.  To get around this you simply need to reconnect to the database each time you spawn a process or thread.

What does this look like in code?

Before (bad) :

After (GOOD):

 

And that’s pretty much it.

My use case for this is that i have a rails app using STI.  I’ve got an array of results from all of my inheriting models and i’d like to sort them ALL based on a common attribute they share — a zip code.

Here is a RIDICULOUSLY simple way to use Ruby to sort the entire array — please note though, the code is querying for “Model.all” just to make the example simple — in my REAL code i have more complicated search criteria.