Code or Die Welcome to my kitchen

Yak shaving tips, Infosec, Astronomy, Gardening

This is my blog. Proud guardian of three cats and two dogs, I currently reside in San Antonio, Texas.

I'm interested in gardening, astronomy, infosec among other pursuits. About this site.

Past and current projects.. You can also browse the blog archive if you like.

Message of the Day: Under construction

Book Review - Ruby Under a Microscope

Ruby Under a Microscope is a great primer into understanding the underlying mechanics of the Ruby language. It demystifies the “magic” surrounding seemingly simple things, for example parsing a string, and I’m a big fan of demystification.

Ruby Under a Microscope, An Illustrated Guide to Ruby Internals by Pat Shaughnessy

This book for me hits the sweet spot between technical aptitude and plain language for both advanced and intermediate levels of study.

What lies beneath (spoiler: it’s C)

Starting out as a novice programmer it is usual to hear that Ruby is an interpreted language. As an interpreted language1, it does not require a compilation step before or at runtime to turn the high-level expressions you’ve written in the text editor into machine-readable bytecode.

However, lurking below this simple explanation is the Matz’s Ruby Interpreter2 (MRI, also called CRuby for reasons that will be quickly apparent,) a series of interpreters which do the heavy lifting when it comes to reducing and interpreting code. It accomplishes this using C and YARV (Yet Another Ruby Virtual Machine3). This applies to Ruby 1.9 and onwards when YARV was merged into Ruby core; before this Ruby was much slower due to the fact that implementation, still in C, was designed as a single-pass interpreted language.

In this capacity, C is acting as an intermediate language4, as it takes the source code of a Ruby program and translates it into a form more suitable for generating object or machine code.

In the book, author Pat Shaughnessy takes us through the life cycle of a simple Ruby “Hello World” program. In excruciating detail we are shown how MRI takes apart each character in a Ruby program and determines the syntax rules to apply. We are also shown the control flow of MRI as the pointer moves through the stack. I found this really fascinating and useful to know.

Hash optimization in Ruby 2.0

Apart from displaying the process broken down into atomic chunks, the book does a great job of showing us how to benchmark and test pretty much anything related to the Ruby language. My favorite example of this is the experiment (Experiment 7-2) on how fast Ruby is able to insert key-value pairs into a hash and comparing this speed in Ruby 1.8 as opposed to 2.0 (with the aforementioned YARV in place.)

The results are surprising. To my understanding, in Ruby 1.8 there is a measurable time lag every 67th key/value pair insertion that is due to bin allocation being performed behind the scenes. Ruby has to “re-size” the amount of memory or bins needed to fit the key/value pairs. In Ruby 2.0, YARV optimizes this allocation. For hashes under 7 elements, hash data is simply saved in an array, known as packed hashes. Once you try to insert the 7th key/value pair, this packed hash is discarded and the actual hash function is called and bins allocated. In this way, hash tables are sized automatically by Ruby, which evenly distributes values across bins and minimizes collisions, (collisions here refers to having to dig into a bin to get a value, i.e. bins are kept shallow.)

Last words

All in all, the book is a very valuable and eye-opening account on the innards of Ruby. There are chapters on JRuby, and metaprogramming. In future editions it will be interesting to see if new developments will be covered. I hope so.

I liked this book because it forces one to reckon with reality and think beyond interpreters, beyond the simple I/O of programs. We know we are getting “hardcore” when we start delving into computer science topics such as abstract syntax trees, memory allocation, pointers, etc. All these concepts have been abstracted away in our modern interpreted languages. This is an amazing feat in of itself, nonetheless I would recommend this book to anyone with more than a passing interest in coding and programming or computer science, even if Ruby is not the language of choice. Here’s why:

In any programming language one will find the same fundamental building blocks of computer science. This book inspired me to dig deeper and was one of the main reasons I decided to learn more fundamental computer science topics and even learn C itself!

I recommend starting with any of the freely available online university introductory computer science courses, particularly Stanford’s5 or Harvard’s6.

Command Line Image Optimization

ImageOptim-CLI is a handy little command line tool (OS X only) by Jamie Mason / @fold_left that incorporates ImageAlpha, ImageOptim and JPGmini. It can be automated as a git pre-commit hook for fire and forget front end development, something I’ve found useful.

Pre-Commit Hook

Add hook to: your_project/.git/hooks/pre-commit

images=$(git diff --exit-code --cached --name-only --diff-filter=ACM -- '*.png' '*.jpg')
$(exit $?) || (echo "$images" | imageoptim && git add $images)

JPGmini does carry a price tag at the Mac App Store, so one would need to evaluate if regular use of JPEG calls for it. On the other hand, open source projects ImageAlpha and ImageOptim are pretty much the best tools in class when dealing with PNG.

I took the tool for a spin on this blog. Even though it is not image heavy, over the course of incarnations I have had a few images accumulate in my public/images folder, currently at a modest 1.5 MB after subtracting sundry animated GIFs. (There are many volumes worth of material to be written about animated GIF optimization and compression, so I will stick to JPEG and PNG for now.) I obtained some large PNG and JPEG images to give a good sample size of 37.8 MB on our test run.

After hitting it with ImageOptim-CLI, the folder’s size became 16.2 MB, a savings of 21 MB or 42.9%. Not too shabby. Now you get to figure out the presentation of your images. Since this is Jekyll, perhaps something simple like using Front Matter to create a photo gallery as in this tutorial: Jekyll Tips. Since we are not using the image gallery anywhere else, it makes sense to do this instead of creating a Jekyll Collection.

Add the image list to the Front Matter on this post:

---
  images:
    - image_path: /assets/img/PNG_JPEG/png_wolf_by_itsdura-d3cle9k.png
      title: PNG Wolf
      link: http://itsdura.deviantart.com/art/PNG-Wolf-202552184
    - image_path: /assets/img/PNG_JPEG/apple.png
      title: Apple
    - image_path: /assets/img/PNG_JPEG/coffee_beans.png
      title: Coffee Beans
    - image_path: /assets/img/PNG_JPEG/GDEM-10km-colorized.png
      title: ASTER image of world
    - image_path: /assets/img/PNG_JPEG/Pillars_of_Creation.jpeg
      title: Pillars of Creation
    - image_path: /assets/img/PNG_JPEG/Star-forming_region_S106_%28captured_by_the_Hubble_Space_Telescope%29.jpg
      title: Hubble star-forming region S106
    - image_path: /assets/img/PNG_JPEG/Japan.jpg
      title: Japan
    - image_path: /assets/img/PNG_JPEG/Cat.jpg
      title: Domestic Cat
---

Then we add code on the post to loop through the array of images. We can even add links to each image to make them clicky, (a highly technical SEO term.)

  <ul class="photo-gallery">
    {% for image in page.images %}
      <li>
        <a href="{{ image.link }}">
          <img src="{{ image.image_path }}" alt="{{ image.title}}"/>
        </a>
      </li>
    {% endfor %}
  </ul>

(Incidentally I learned about using the raw tag for Liquid templates inside code blocks.) Finally, the CSS to present the images:

.photo-gallery, .photo-gallery li {
  list-style: none;
  padding: 0;
}

.photo-gallery li {
  display: inline-block;
  width: 33%;
}

.photo-gallery li img {
  width: 100%;
}

The finished result is a quickly-loading, easily-configurable gallery of optimized photos:

I really liked using ImageOptim-CLI and would recommend it to automate the process. Even when not using this command line tool at all, image optimization should be a part of every front end developer work flow.

In closing, ImageMagick must be mentioned as the gold standard of open source command line image manipulation. It allows amazingly fine grain control and has excellent documentation well beyond the scope of this humble post.

Docs:

Natural Philosophy Redux

There is a grand adventure to be embarked upon every time a student asks a question and the mentor says: “I don’t know, let’s find out.” The chance to learn occurs at the moment of uncertainty by forcing the knowledge seeker to step out of their comfort zone and expose ignorance. Own ignorance. Keep uncovering new uncertainties to investigate.

With regard to convention and standards, become proficient enough with your tools to recognize the value that is being added by following them. Promptly try to break the rules to see what happens. Find out why you are told to do things a certain way, then figure out how to do it better.

Respect elders and tradition while at the same time questioning authority, institutions, and customary manners of doing things. Wise elders when questioned thusly will recognize the positive value of reinforcing fundamentals and the chance however small of learning something completely new. The greatest scientific breakthroughs have been made from examining established beliefs and taking an alternate tack.

Break down the pillars of beliefs to atomic constituents. Build them back up by carefully considering each addition, being ruthless in the inquisition of your tenets. Hold no belief so fanatically that contravening evidence is ignored. Be fluid in ways.

Finally, recognize the struggles that others have around you. Help them. You will probably learn something in the process. Voraciously consume resources and be mindful to give credit. Strive to be a good citizen steward of code and contribute to open source software projects. Give and receive feedback as a gift. Be grateful to be able to see this episode of history from your particular unique vantage point.

Remember to be human, and to step out of your skin sometimes.