Matthew Hinton

Just where I spout off on whatever I feel like

Month: April 2012

A Bibliophile Learns to Love Ebooks

I have been a book lover since the third grade when I read my first sizable book. I remember it was a novelization of Disney’s “The Fox and the Hound” and it was a whopping two hundred pages long. I remember being proud to have read such a long book. Then in middle school I read “The Hobbit” and discovered Fantasy and Science Fiction. I became a voracious reader of the those genres. For much of my life there was nothing I liked better than to have a book in my hand and spend my time reading. I loved to go to the bookstore and wander through the isles looking for my next book. I have accumulated so many books over the years I was starting to drown in them. So after some thought I made the decision to get a Kindle and migrate some of my reading to it.

This has worked well for me over the past fifteen months. I think I have even saved money because there are a lot of sales on ebooks and I have read a bunch of self published authors whose books are usually only a couple of dollars at most. I have enjoyed the large selection and for the most part the reasonable pricing of Amazon’s ebooks.

Over the past year aside from buying a few books from my favorite authors in hardcover most of what I have bought have been ebooks on my Kindle. This trend accelerated once the Borders Books chain shutdown. Borders was my favorite bookstore and I was truly saddened to see them go out of business. Now that my primary bookstore is Amazon I either buy an ebook or I throw the book in my wish list as a way to keep track of it for possible purchase at a later time.

Unfortunately as my buying habits become common across the public it means the end of physical books. Even though I still buy some physical books the economics are against that publishing model. I fear that my kids will have a totally different reading experience than I have.

Ruby Sort a Subset of Array Elements

Here is a little trick I came up with for sorting a subset of array elements in Ruby.

I needed to sort a subset of elements in an array while leaving the other elements in the positions they occupied.

The algorithm I used was to create a new array with just the items I want to sort. Sort the new array’s elements. Then you have the original array that contains all the elements and a second sorted array with just the items you are interested in. The original array and the sorted array both contain the same subset of items then you want sorted. So go through the original array and whenever you find one of the items you wanted to sort replace it with the next item from the sorted array.

Ruby array subset sort

 all_elements = [1, 'b', 2, 'a']
    subset = all_elements.select {|i| String === i}
    subset.sort!
    all_elements.map do |i|
        if i.kind_of?(String)
            subset.shift
        else
            i
        end
    end

© 2020 Matthew Hinton

Theme by Anders NorenUp ↑