Archive for April, 2011

My Sweet Caroline

This is just a fun movie that I took on Easter, 2011, of my wife playing with my Sweet Caroline. It shows her wonderful laugh and interaction at this great age.

Sweet Caroline on YouTube

Ruby Enumerable#partition

More than once in the past I would have used this, had I known it was there. Often I am consumed by the task of getting the job done and less focussed on keeping up to date with the documentation. Using partition you can split objects into two groups fairly easily. In this example, I’ll take two use cases for it. The first one is simply a way to understand it well, while the second will be a possible usage scenario.

Here, we will take a finite number, 10 and split it into a set of even and odd numbers. We will assign them to the respective variables, odd and even. I am doing this in IRB, which will show you the version of Ruby that I am executing in:

ruby-1.9.2-p180 :001 > odd, even = 10.times.partition { |x| x % 2 == 1 }
 => [[1, 3, 5, 7, 9], [0, 2, 4, 6, 8]]
ruby-1.9.2-p180 :002 > odd
 => [1, 3, 5, 7, 9]
ruby-1.9.2-p180 :003 > even
 => [0, 2, 4, 6, 8]
ruby-1.9.2-p180 :004 >

Now let’s take a quick look at using this with an Object:

  1. Add the class
  2. ruby-1.9.2-p180 :005 > class Person
    ruby-1.9.2-p180 :006?>   attr :sex
    ruby-1.9.2-p180 :007?>   def initialize( sex = "f" )
    ruby-1.9.2-p180 :008?>     @sex = sex
    ruby-1.9.2-p180 :009?>     end
    ruby-1.9.2-p180 :010?>   end
     => nil
    
  3. Add some Person objects to our people array
  4. ruby-1.9.2-p180 :013 > people = []
     => []
    ruby-1.9.2-p180 :014 > people << Person.new("m")
     => [#<Person:0x000001013084d0 @sex="m">]
    ruby-1.9.2-p180 :015 > people << Person.new("m")
     => [#<Person:0x000001013084d0 @sex="m">, #<Person:0x000001012fc018 @sex="m">]
    ruby-1.9.2-p180 :016 > people << Person.new
     => [#<Person:0x000001013084d0 @sex="m">, #<Person:0x000001012fc018 @sex="m">, #<Person:0x000001012f7810 @sex="f">]
    
  5. Partition our people based on sex
  6. ruby-1.9.2-p180 :017 > men, women = people.partition { |p| p.sex == "m" }
     => [[#<Person:0x000001013084d0 @sex="m">, #<Person:0x000001012fc018 @sex="m">], [#<Person:0x000001012f7810 @sex="f">]]
    ruby-1.9.2-p180 :018 > men
     => [#<Person:0x000001013084d0 @sex="m">, #<Person:0x000001012fc018 @sex="m">]
    ruby-1.9.2-p180 :019 > women
     => [#<Person:0x000001012f7810 @sex="f">]
    ruby-1.9.2-p180 :020 >
    

    This is a fairly easy demonstration of Enumerable#partition, and if you’re like me, once you see it, you’ll never forget it. It’s a fun one to work with for sure!

Jenkins CI

Welcome to Jenkins CI! | Jenkins CI.

I am nothing but impressed with Jenkins-CI.  This tool has simplified my life and worries on the web.  I love more than one thing about it, and use it in more ways than it was intended.  I run all of my past crontab functionality in it and can now see the forecast easily on the web!

Riding Rails: jQuery: New Default

Riding Rails: jQuery: New Default.

This is timely.  I was getting a bit too used to adding jQuery to every project and removing prototype.  Needless to say, I’m excited at the prospect and hope to see Rails 3.1 soon!

© 2012 - Jeff Ancel
Wordpress Themes
Scroll to Top