Posts tagged ‘ruby’

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!

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!

Keep Multiple Ruby Projects Separated

RVM LogoHow do I keep multiple Ruby projects separate?.

This is really helpful article for those of us that have many projects that span great amounts of time.  It gives you the high level of how to leverage RVM, with Bundler for managing deployment recipes on servers that may have different versions of everything for each project.

I am going to probably build on this with an example, since I need to use this technique on my production server.  Stay tuned, as he states in the article, I always have much more going on than I can actually accomplish, so hope to do this on the next weekend.

© 2012 - Jeff Ancel
Wordpress Themes
Scroll to Top