What the heck...

Chris Shaffer's Development Life (or at least as much as he posts about it)

TextBox

Tag cloud

    RecentPosts

    Ruby and Immutability

    Just a quick note. It occurred to me this morning that with how dynamic Ruby is (being able to add methods/properties to an instance of a class, etc), it is probably difficult to make an object immutable.

    Not so - Ruby supports immutability, though it does it in a slightly different way (which is at a glance both better and worse than what I am used to in C#). In Ruby, immutability is assigned at the instance level by calling the freeze method.

    As I said, this seems both good and bad. On the bad side, as a consumer of a potentially frozen object I have to either test each object (using the frozen? method) before attempting to change or risk throwing an exception. On the good side, the freeze method is enforced by the run-time; In C# I can write a class meant to be immutable, but this really amounts to specifically designing the class not to have the capability to change rather than telling the compiler "I want this class to be immutable".

    Anyway, just another interesting aspect of Ruby. I'll have to write some code using freeze to get a better feel for it.


    Categories: Development | Ruby
    Permalink | Comments (0) | Post RSSRSS comment feed

    Beginning Ruby

    Well, I'm messing around with Ruby at home for the most part -- haven't really experimented much more with IronRuby at work. I'm continuing to read the Rails book I have, though I think I will wait to start messing around with Rails until I have a better feel for Ruby itself. Learning the language itself hasn't presented any real challenges, however I have a fair ways to go before I am writing idiomatic Ruby code.

    To work on learning the language, I've decided to use my intermittent computer free time to go through www.rubyquiz.com.

    To that end, I spent a good deal of time (more than I would have expected) working on the first quiz (implementing the "Solitaire Encryption" method).

    I started out with a simple Encoder class with a single encode method, and a TestEncoder class that ran the encode method and verified the result using the example from the quiz. As I started filling in the encode method, I started breaking things out into separate classes and added tests for each of those classes. It felt a lot easier to do with Ruby than it has in the past in C# to do these things, though I can't really point at any single reason why that might be.

    My final solution ended up with the following classes: Deck, Keystream, Encoder, Decoder. Most of the time I spent coding up the solution went into the Deck class, which includes methods like shift_right, triple_cut, and count_cut. Looking at the code now, I think it seems a little odd that I wrote each of those methods to return an array; I did that because it made the testing a little easier (I could instantiate a single deck and then have several asserts that all assumed the deck was starting out clean, instead of either instantiating a new deck before each assert or having each assert work on the deck dependent on the previous statements). Once I had the methods implemented and testing correctly, I added a ! version that actually modified the deck.

    Remaining now is to go through the other solutions that were submitted to the Ruby Quiz site to learn some of the ways I can improve my Ruby code. Just glancing at one example showed me a potential key-saver -- there is an array indexer that takes two numbers or a range and returns a slice of the array. I used the slice method a lot, and also spent a good bit of effort on the math required to get the numbers to pass to it; Statements like:

     
     
    return cards.slice(0, sstart) + @cards.slice(sstart + 1, num) + [val] + @cards.slice(send + 1, @cards.length - send - 1)
     
    

    Will both look better and be easier to work on if they looked more like:

     
     
    return cards[0..sstart] + @cards[sstart + 1, num] + [val] + @cards[(send + 1)..-1] 
     
    

    (I haven't tested that line, just quick replacement). Not only do I get rid of good set of length, but using "..-1" makes a lot more sense than "@cards.length - send - 1". It's a relatively trivial change, but worth making.

    Somewhat related, but as part of this I also did some work on getting a dev environment set up on my laptop; I installed many of the things mentioned in a post on thoughtbots (found with a little googling). I'm using zsh now (don't really know any of the differences between this and the default yet, but digging the prompt including the current git branch that the referenced config files gave me) and MacVim (started with just regular vim that came with OS X, but decided to try a different one when I saw that the default was compiled with -ruby...May go back to regular vim but compile it myself, we'll see what happens).

    Also made a Rakefile for this project, with only one target (test). I haven't spent any time learning the Rakefile syntax yet, but the little I have now (copied and pasted from the Ruby Koans project if I remember right) looks nice (Ruby syntax version of make?).


    Categories: Ruby | Development
    Permalink | Comments (0) | Post RSSRSS comment feed