Posts tagged ‘irb’

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 <<"m")
     => [#<Person:0x000001013084d0 @sex="m">]
    ruby-1.9.2-p180 :015 > people <<"m")
     => [#<Person:0x000001013084d0 @sex="m">, #<Person:0x000001012fc018 @sex="m">]
    ruby-1.9.2-p180 :016 > people <<
     => [#<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| == "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!

Show ActiveRecord SQL in Rails 3 Console

If you want to show sql in your console for Active Record, then you may have run across the past Rails Recipes book or similar formulas for Rails < v3.0.  I just tweaked a formula I saw in that book to work with Rails 3 and here it is (This is designed to work on a Mac or most Linux flavors, Windows users, YMMV):

Create ~/.irbrc or add the following to your .irbrc, Mine is located at ~/.irbrc

if defined?(Rails) && Rails.env
  load File.dirname(__FILE__) + "/.railsrc"

Create ~/.railsrc or add the following to your ~/.railsrc:

def toggle_sql
  if ActiveRecord::Base.logger
    set_logger nil and return false
    set_logger and return true

def set_logger(logger)
  ActiveRecord::Base.logger = logger
set_logger nil # start with the normal logger

Then, when you’re in your rails console world, just call toggle_sql. It returns true if the sql will be on, and false if the sql will be off.

Please feel free to leave suggestions and improvements here as I am always open to these suggestions.

© 2012 - Jeff Ancel
Wordpress Themes
Scroll to Top