Ruby: December 2005 Archives

% ruby -v -ryaml -e 'y({:a => 2})'
ruby 1.8.4 (2005-12-22) [powerpc-darwin8.3.0]
--- 
:a: 2

Confirmed kill!

This should fix all 1.8-related rubygem compatibility issues that occurred in 1.8.3.

Inline::ObjC

| | Comments (0)
Nuno Cruces provided me the following gem (hacked up by yours truly):
require 'inline'

class Inline::ObjC < Inline::C
  def initialize(mod)
    super(mod)
  end

  def import(header)
    @src << "#import #{header}"
  end
end

class MyClass
  inline(:ObjC) do |builder|
    builder.import "<Foundation/NSString.h>"
    builder.add_compile_flags '-x objective-c', '-framework Foundation'

    builder.c %q{
      void test() {
        printf("%s\n", [@"Hello World!" cString]);
      }
    }
  end
end

MyClass.new.test
I love it... OSX hackers, have at it...

Splat is good for you

| | Comments (2)
A recent post on projectionist shows two code examples:
  def foo(*args)
    [args].flatten.map do |arg|
      # ...
    end
  end

  def foo(*args)
    Array(args).map do |arg|
      # ...
    end
  end
and argues their (ugly) necessity. I think both examples fail to hit the mark (or there is a typo?) as they've ensured the initial codition simply by using *args. Allowing them to state the problem in a straightforward fashion:
  def foo(*args)
    args.flatten.map do |arg| # if you are trying to combine a bunch of arrays
      # ...
    end
  end

  def foo(*args)
    args.map do |arg|
      # ...
    end
  end
which, is much more natural. If I had to guess, I'd say they meant to not put the splat in front of args. I find using splat to be a better fit.

About this Archive

This page is a archive of entries in the Ruby category from December 2005.

Ruby: November 2005 is the previous archive.

Ruby: January 2006 is the next archive.

Find recent content on the main index or look in the archives to find all content.

Pages

Powered by Movable Type 4.1