Cowell Computer Consulting

Installing rmagick on OS X

For some reason the gem tries to build against a PPC library, which on my intel machine, doesn't not work.

sudo su -
ARCHFLAGS='-arch i386' gem install rmagick

Note: you might need to fix the quotes.

Posted by Luke Cowell on June 01, 2009 at 09:59 PM

Comments: 1 (view/add your own) Tags: OSX, ruby

Contributing to the Community

I'm proud to announce I made my first real contribution to the ruby / ROR community. I added a current quiz feature to the ruby quiz site and added an RSS feed to the site for all quizzes and solutions.

Announcement http://www.ruby-forum.com/topic/183984#809279

Ruby Quiz RSS Feed: http://rubyquiz.strd6.com/quizzes.rs

Ruby Quiz Site: http://rubyquiz.strd6.com/

Posted by Luke Cowell on April 23, 2009 at 11:13 PM

Well that's Iterating

A great page explaining different iterators available in ruby.

Read about it here.

Posted by Luke Cowell on March 20, 2009 at 03:43 PM

Comments: 0 (view/add your own) Tags: ruby

Ruby Quiz #187

I saw this solution on Ruby Quiz and I thought it was brilliant. I thought I'd explain how the original author came up with this beautiful looking tree to generate some code.

          ###  M E R R Y   M A T Z M A S  ### 
                          "/|"\ 
                      "/wl)e";eval(\ 
                         "A | A"\ 
                        "A  |  A"\ 
                            \ 
                         "InHhL"\ 
                        ",Z,%w{-"\ 
                       ",Y&/LH,L*"\ 
                      "cCnd,Hh&/d,"\ 
                     "YCB/Hh,Y&YHh,"\ 
                    "L&xHh,L*v*nHh,*"\ 
                   "&ghHh,n&nHh,H*nHh"\ 
                  ",*q*v*nHh,Hw*qv*Hh}"\ 
                 "Iq&n*L,Z,[,n&qK,n&qK,"\ 
                "@TwC,LH/&ng,gLBbL@K,@Th"\ 
               "/**,YC/k*d,Hh/*QdL@K,@FCB"\ 
              "/,cQqq&ng,=/CcL@K,@F&v*,RBb"\ 
             "y,g*mL!@K,@S&x,mQ/LhQqL,dBm=&"\ 
            "ng@K,@S*v*n,dBckL,Q-Hy=&ng@K,@E"\ 
           "&ghH,d&/L,Q-gqCbb&ng@K,@N&n*,/Qng"\ 
          "*L,LH*==&ng@K,@T*n,H/yL,Q-cQHch&ng@"\ 
         "K,@Eq*v*n,&CL,=&=&ng@K,@Tw*qv*,mCnk*y"\ 
        "L,=QHch&ng@,]IDAJJA2)J*Qch,dC,U&UI=/&nH"\ 
       ",@\nOn,Hh*,#{nHhL[&]},dQy,CY,MQHzmQL,my,"\ 
      "H/B*,qCv*,gQv*,HC,m*:\n@I&JdCwnHCD2),dC,UjU"\ 
     "I=/&nH,@,,,#{q&n*L[j]}\n@I*ndI=/&nHDD&,ZZ,A)"\ 
                         "?,@,,"\ 
                         ",G@,:"\ 
                         ",@,,,"\ 
                         "Gnd,Q"\ 
                         "@)I=/"\ 
           "&nH,@,n*w,v*/L&Cn,CY,RBby!\n@I*nd"\ 
            "I".tr(' ID/VHLYGBqCA&|*UQJ=Z@K,', 
              '+;(r/tsfAulo1i8e|a.p=", '))

Here's the original code: http://pastie.org/344030.txt

What it outputs is something like this:

On the first day of Matzmas my true love gave to me:
   A new version of Ruby!

-snip-

On the twelveth day of Matzmas my true love gave to me:
   Twelve monkeys patching
   Eleven ios piping
   Ten trys a-catching
   Nine ranges stepping
   Eight dirs a-globbing
   Seven ducks a-typing
   Six marshals dumping
   Five Ruby gems!
   Four calling procs
   Three forked threads
   Two string gsubs
   And a new version of Ruby!

Well everything up to the 'eval' is fluf, so we can ignore that. If we paste everything inside the eval without calling the eval into irb, we get:

"1+8+11++8++1;nths = %w{- first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelveth};lines = [ nil, nil, \"Two string gsubs\", \"Three forked threads\", \"Four calling procs\", \"Five Ruby gems!\", \"Six marshals dumping\", \"Seven ducks a-typing\", \"Eight dirs a-globbing\", \"Nine ranges stepping\", \"Ten trys a-catching\", \"Eleven ios piping\", \"Twelve monkeys patching\" ];(1..12).each do |i|;print \"\nOn the #{nths[i]} day of Matzmas my true love gave to me:\n\";i.downto(2) do |j|;print \" #{lines[j]}\n\";end;print((i == 1)? \" A\" : \" And a\");print \" new version of Ruby!\n\";end;"

So we see that the tr function generates the above code and turns it into some ruby code that can be passed to eval. We can also ignore everything up to nths.

tr(' ID/VHLYGBqCA&|*UQJ=Z@K,', '+;(r/tsfAulo1i8e|a.p=", ')

The code roughly cleans up to this:

nths = %w{- first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelveth};
lines = [ nil, nil, "Two string gsubs", "Three forked threads", "Four calling procs", "Five Ruby gems!", "Six marshals dumping", "Seven ducks a-typing", "Eight dirs a-globbing", "Nine ranges stepping", "Ten trys a-catching", "Eleven ios piping", "Twelve monkeys patching" ];
(1..12).each do |i|;
  print "\nOn the \#{nths[i]} day of Matzmas my true love gave to me:\n";
  i.downto(2) do |j|;
      print "   \#{lines[j]}\n";
    end;
    print((i == 1)? "   A" : "   And a");
    print " new version of Ruby!\n";
end;

So, if we reverse the 'tr' command we can encode similar blocks of code. Here's an example:

     "asdasd"
    eval("DAJJA0)"\
  "J*Qch,dC,UnUI,=/"\
  "&nH,@I,cQn,cCBnH,H"\
 "C,Hh*,nBmb*/,\#{n}\n@"\
        "I*ndI"\
.tr(' ID/VHLYGBqCA&|*UQJ=Z@K,'\
,'+;(r/tsfAulo1i8e|a.p=", '))

Ta-da! It's a lumpier, uglier version of the tree that outputs some pretty unimpressive code.

Posted by Luke Cowell on December 22, 2008 at 11:57 AM

Comments: 0 (view/add your own) Tags: ruby