map_by_method - the final announcement

Posted by Dr Nic on September 07, 2007

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

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Chris Sat, 08 Sep 2007 02:11:30 UTC

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

  2. clem Sat, 08 Sep 2007 17:09:19 UTC

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

  3. Dr Nic Sun, 09 Sep 2007 09:05:57 UTC

    @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 Sun, 09 Sep 2007 09:08:18 UTC

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

  5. [...] map_by_method - the final announcement [...]

  6. lawrencepit Thu, 15 Nov 2007 07:16:43 UTC

    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

  7. Dr Nic Fri, 16 Nov 2007 05:44:02 UTC

    @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
    
Comments