Dr Nic

map_by_method – the final announcement

I don’t really talk about my projects after I release them except to show off fancy new things, like newgem sporting the new RubiGen generator.

So I don’t know why I give updates here about one of the smallest projects – map_by_method.

Probably, its because it doesn’t deserve a Google Group or even a webpage really. It should probably be integrated into activesupport gem or something.

Except, it just never seemed to 100% work.

That is, it go more blog coverage than anything else, but wasn’t always useful. Yeah, a like Miss South Carolina.

BUT Version 0.8.2 is now out. It fixes – I believe – all known problems – I think.

gem install map_by_method

In your code (or config/environment.rb for rails):

gem 'map_by_method', '>=0.8.2'
require 'map_by_method'

I added a few more iterator methods too: sort_by, group_by, and index_by.

>> Conference.find(1).conference_sessions.group_by_from
=> returns all MyConfPlan conference sessions grouped by start time, for RailsConf2007

MyConfPlan…

Oh yes it sold. Announcement coming soon.

Plus, a link to the code – as it is being open sourced by its new owner. Sweet!

Well… sweet for you. I already had access to the code.

Next on the todo list

Add something similar to Ambition by Err “we-write-so-many-cool-projects-I-don’t-know-how-we-find-time-to-maintain-them-all” the Blog, Chris and PJ.

I’d kind link something like:

Conference.select { |c| c =~ "Rails").sort_by_name

Which would behave like:

>> Conference.select {|c| c.name =~ /Rails/}.sort_by(&:name).to_sql
=> "SELECT * FROM conferences WHERE conferences.\"name\" ~ 'Rails' ORDER BY conferences.name"

Oh well, perhaps we can talk about it during RailsConf Europe, when I get the microphone to talk about such tom-foolery and hi-jinx: Meta-Magic in Rails: Become a Master Magician

Related posts:

  1. MyConfPlan sale – less than 12 hours to go The auction for MyConfPlan has less than 12 hours to...
  2. map_by_method now works with ActiveRecord associations I was always annoyed that map_by_method was broken for ActiveRecord...
  3. Welcome to the future Welcome to anyone visiting from DHH’s blog and other corners...
  4. map_by_method now increasingly more niftier-er. Recap on map_by_method magic: >> p.account.transfers.map_amount # amount method mapped...
  5. Railsconf 2007 – who’s coming? It probably won’t mean you get free tickets, and if...

7 Responses to “map_by_method – the final announcement”

  1. Chris says:

    You’re gonna steal my schtick? I see how it is.

  2. clem says:

    Trying to gem update map_by_method to 0,8.2 is throwing a zlib::buferror on my winsuck machine.

  3. Dr Nic says:

    @dem – sounds like a rubygems issue. If it continues, try uninstalling all copies of the gem, and reinstalling. If that fails, try checking the rubygems mailing lists.

  4. Dr Nic says:

    @chris – nay, my clever intercontinental colleague, it is a contribution or collaboration, rather than a corruption or conspiracy.

  5. lawrencepit says:

    Dear Dr Nic,

    Is there a particular reason why you don’t pass the arguments and block magic to the mapped method? For more fun I modified line 33 to:

    self.send(iterator) { |item| item.send(method, *args, &block) }

    and ran these tests:

    require ‘map_by_method’
    a = ["hello world", "hallo wereld", "ciao mondo"]
    puts a.map_by_capitalize
    puts a.map_by_center(30)
    a.map_by_scan(/^(.*)\s(.*)$/) {|x,y| print y, “.”, x, ” — ” }

    which outputs:

    Hello world
    Hallo wereld
    Ciao mondo
    hello world
    hallo wereld
    ciao mondo
    world.hello — wereld.hallo — mondo.ciao –

    performance wise this gem works pretty well. Much nicer magic this compared to Symbol#to_proc. :-)

    Best regards,
    Lawrence Pit

  6. Dr Nic says:

    @lawrencepit [via] – ooh nice one. v0.8.3 released.

    Here are the unit tests I added:

      def test_pass_args
        original = ["hello world", "hallo wereld", "ciao mondo"]
        expected = %w[hell hall ciao]
        assert_equal(expected, original.map_by_slice(0,4))
      end
    
      def test_pass_block
        original = ["hello world", "hallo wereld", "ciao mondo"]
        expected = ["[hello ]world", "[hallo ]wereld", "[ciao ]mondo"]
        assert_equal(expected, original.map_by_gsub(/^(.*)\s/) {|first_word| "[#{first_word}]" })
      end