Ruby: assign boolean logic result to variable

Or: Why the hell?

This would be a more suitable title. Recently I ran in several potholes that took me ages to figure out. Well, usually locating the mistake in an grown code base accounts for most of the “lost time”. Replicating the issue is another factor. I often encounter issues that occur seemingly random. Some would throw huge rocks, engraved with the words ‘testing’, at various body parts of me. I like tests. I actually do tests. Well, on most parts anyway. But in this case it was in some stupid, 10 lines of code daemon-launcher.

I love making mistakes. Searching for them will force you to do several things you should’ve done while coding. I’m not talking about an missing “;” or “end”. I’m talking about some wrong behavior in certain circumstances. Code gets DRYed and refactored because nobody wants to debug complex methods. You also (re)write tests for everything involved. I often find myself writing tests for the expected behavior but don’t spend enough time to think about other cases.

I digress. Todays pothole (simplified)


a = 2
b = (a > 20 and a <= 23) or (a >=0 and a <= 4)

b => false

WTF? To make this more clear:


a = 2
b = (a > 20 and a <= 23) 
c = (a >=0 and a <= 4)
d = b or c
d => false

Funny though:


a = 2
b = (a > 20 and a <= 23) 
c = (a >=0 and a <= 4)
d = c or b
d => true

Aha! Turns out, I did the assignment wrong. This one works as expected:


a = 2
b = (a > 20 and a <= 23) 
c = (a >=0 and a <= 4)
d = (b or c)
d => true

Apparently in this case you need the braces. No idea why though. Please enlighten me.

On Delphi Exception raising, re-raising and try-except blocks

Took me nearly 3 days to locate the error in following code:


procedure DoSomething;
  try
    something that may raise an exception
  except
    on e: EMyError1 do
      raise e // to re-raise the thing
    on e: Exception do // everything else
      raise EMyError2.Create('something happend');
  end;
end;

procedure WrapperForDoSomething;
begin
  try
    DoSomething;
  except on E: Exception do
    ShowMessage(e.message);    
    DoSomethingElseWith(e);
    Log(e.message);
  end;
end;

You see it, right? Yeah, me neither. Well at least I do now. I got an “Invalid Pointer Operation” right after Log(e.message). “E” was (still) accessible in the Log-Function. But was Free-ed long ago.
Where do you ask? Right after the first except block in DoSomething. I didn’t know this behavoir existed. It’s kind of logical, now, to that this local variable in the try-except block should be destroyed afterwards.

Simple fix:
instead of


...
on e: EMyError1 do
raise e // to re-raise the thing
...

do this:

try

except
on e: EMyError1 do
raise EMyError1.create(e.message); // to re-raise the thing in the correct way

Update
Turned out that I’m wrong (or not efficient enough) on this one again. Comments & Reddit tought me the correct way:


try
...
except on e: EMyError1 do
  raise; // to re-raise the thing in the correct way
end

I’m working in a multithreaded environment and I was looking 2 of the 3 days in the wrong direction because I thought it had something to do with the (lack) of CriticalSections or Synchronization. Well – turned out to be dead simple.

Nokogiri on OSX Mountain Lion

After upgrading from my 2009 MBP to a 2012′ Mac Mini with the awesome ‘Migration’ tool I found myself pretty much in the same environment as before.

The only thing not working as expected was the failing bundle command. (on the nokogiri gem). Somehow my homebrew folders had some wrong permissions after the migration. I fixed that by reinstalling it but the gem wouldn’t get built. Well turns out a “simple”

$ gem install nokogiri --
--with-xml2-include=/usr/local/Cellar/libxml2/2.8.0/include/libxml2
--with-xml2-lib=/usr/local/Cellar/libxml2/2.8.0/lib
--with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28
--with-iconv-include=/usr/local/Cellar/libiconv/1.13.1/include
--with-iconv-lib=/usr/local/Cellar/libiconv/1.13.1/lib

left me with the note, that “libxml/parser.h” wasn’t found.

I still can’t wrap my head around this. But apparently it has something to to with the XCode Command Line Tools. (For not being gcc 4-something ).

This came to the rescue:

$ brew tap homebrew/dupes
$ brew install apple-gcc42
$ sudo ln -s /usr/local/bin/gcc-4.2 /usr/bin/gcc-4.2

Running gem install nokogiri or in my case the bundle command works like a charm.

Thx to: Deployment Zone

Speed-up WordPress with CDN and Plugins

It’s not that this site is under heavy load – nor do I expect that to change. But anyways, I tried to ‘Stephen-Fry-proof’ my humble abode.

Maybe my current setup is a bit redundant – but the speed is just awesome right now.

  • Combine and Minify Javascripts and Stylesheets: Better WordPress Minify (Link)
  • Cache all Pages and Posts with: WP-Supercache (Link)
  • And finally I registered to the free Service from Cloudflare (Link)

Cloudflare

DNS and CDN Service with basic threat protection for free. Save Bandwidth (not that it’s expensive nowadays) and block Spammers and Bots before reaching your server. It also mirrors your assets in its own CDN automatically for faster access.

Cloudflare Dashboard

Cloudflare Dashboard

MySQL: unauthenticated user in ProcessList

Today one of my mysql machines stopped working due to many tasks. There were more than 200 of these entries in the process list:

12345 | unauthenticated user | 10.0.1.23:44334 | | Connect | | login

turns out – that the reverse dns resolve wasn’t working (anymore?) with this internal IP.
solved by simply adding the IP and a hostname in /etc/hosts.

RMagick GEM on OSX Lion

After installing ImageMagick with Homebrew I still couldn’t run a
gem install rmagick. Because of this:
Package MagickCore was not found in the pkg-config search path.
got solved by:
cd /usr/local/lib/pkgconfig && ln -s /usr/local/Cellar/imagemagick/6.7.5-7/lib/pkgconfig/MagickCore.pc

No Log to STDOUT from Sinatra / Thin in development?

Today logging broke in my environment ( Rack 1.4 & Sinatra 1.4 (GIT) & Thin 1.3.1 ). Don’t know why exactly (I refreshed all my Gems) – but somehow it got fixed by adding:

use Rack::CommonLogger

Sinatra Streaming + nginx Proxy

Fairly easy. The important line here is:
proxy_buffering off;

I think this one works at least for most Rack based applications. The application itself runs on thin ( besides that, I only know Unicorn and Rainbow(s?) that have Streaming Support. Webrick won’t do that (yet))

My vhost config.


upstream stream {
  server 127.0.0.1:4800;
  server 127.0.0.1:4801;
  server 127.0.0.1:4802;		
  server 127.0.0.1:4803;		
  server 127.0.0.1:4804;  	
}
    
server {
  listen   80;
  server_name  stream.domain.com;
  client_max_body_size 5M;  
  root /path/to/project/current/public;
  error_log	/path/to/project/shared/nginx/stream.error.log;	
  access_log  off;
  location / {		
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_buffering off;
    proxy_redirect off;

    if (-f $request_filename) { 
      break; 
    }
    
    if (!-f $request_filename) {
      proxy_pass http://stream;
      break;
    }
    
  }
}