Category Archives: Ruby

Ruby: hex to dec

Ruby is just so simple:

ruby-1.8.7-p302 > 'ff'.to_i 16
 => 255 
ruby-1.8.7-p302 > 255.to_s 16
 => "ff"
DeliciousTwitterFacebookLinkedInRedditSlashdotTechnorati FavoritesDiggShare
Posted in Ruby | Leave a comment

Building Native Ruby Extensions on Windows

Building native Ruby extensions on Windows requires a little bit of extra work. Here’s how it goes:

  1. Download and install Ruby for Windows (download)
  2. Add the bin folder of your Ruby installation to the PATH environment variable. Open a command prompt and type in ruby -v. That should print the version number of your Ruby installation.
    • If you tried to install / build a native extension now, it’s likely you get such an error message:
      C:\Documents and Settings\Stefan>gem install ruby-debug
      Building native extensions.  This could take a while...
      ERROR:  Error installing ruby-debug:
      ERROR: Failed to build gem native extension.
      C:/Ruby187/bin/ruby.exe extconf.rb
      creating Makefile
      make
      'make' is not recognized as an internal or external
      command, operable program or batch file.
      Gem files will remain installed in C:/Ruby187/lib/ruby
      /gems/1.8/gems/linecache-0.43 for inspection.
      Results logged to C:/Ruby187/lib/ruby/gems/1.8
      /gems/linecache-0.43/ext/gem_make.out
      
  3. In order to fix this, you need to install the Development Kit. Download it here.
  4. Unpack it, e.g. to c:\RubyDevKit
  5. Add C:\RubyDevKit\bin to your PATH
  6. Add C:\RubyDevKit\mingw\bin to your PATH

That’s it, building and installing native extensions works now like a charm

C:\Documents and Settings\Stefan>gem install ruby-debug
Building native extensions.  This could take a while...
Building native extensions.  This could take a while...
Successfully installed linecache-0.43
Successfully installed ruby-debug-base-0.10.4
Successfully installed ruby-debug-0.10.4
3 gems installed
Installing ri documentation for linecache-0.43...
Installing ri documentation for ruby-debug-base-0.10.4...
Installing ri documentation for ruby-debug-0.10.4...
Installing RDoc documentation for linecache-0.43...
Installing RDoc documentation for ruby-debug-base-0.10.4...
Installing RDoc documentation for ruby-debug-0.10.4...

Update:

For detailed documentation please check out these links (thanks to Luis Lavena):
https://github.com/oneclick/rubyinstaller/wiki/Development-Kit
https://github.com/oneclick/rubyinstaller/wiki/Tutorials

DeliciousTwitterFacebookLinkedInRedditSlashdotTechnorati FavoritesDiggShare
Posted in Ruby, Windows | 6 Comments

Serialize / Deserialize nested objects in ActiveRecord

Using ActiveRecord’s serialize method you can save Ruby objects in a single field of your model. The serialization is done through YAML.

Today I tried to save a nested object structure with this method. These are the classes:

class PhotoStyle
  attr_accessor :image
 
  def initialize
    @image = StyleElement.new
  end
end
 
class StyleElement
  attr_accessor :selector, :style
 
  def initialize
    @selector = '*'
    @style = {}
  end
end

The model which saves a PhotoStyle object:

class Photo < ActiveRecord::Base
  serialize :style, PhotoStyle
end

This first approach didn’t quite work out. Saving the model worked fine, but when I loaded it only PhotoStyle got deserialized to the original type. The nested StyleElement didn’t:

p = Photo.last
p.style
=> #<photostyle:0xb70a906c @image=#<YAML::Object:0xb72079a4 @ivars={"selector"=>"#theimage", "style"=>{"border"=>"0", "box-shadow"=>"1px 3px 15px #555"}}, @class="StyleElement">}, @class="PhotoStyle">>
</photostyle:0xb70a906c>

Note the YAML object assigned to the @image instance variable.

I'm not sure if that's a bug in Rails3 or if that is by design. You can however "fix" it by running this code snippet when your application starts up:

YAML::Syck::Resolver.class_eval do
  def transfer_with_autoload(type, val)
    match = type.match(/object:(\w+(?:::\w+)*)/)
    match && match[1].constantize
    transfer_without_autoload(type, val)
  end
  alias_method_chain :transfer, :autoload
end

Save it e.g. in yaml_autoloader.rb in the config/initializers folder of your app.

Now all types will autoload:

#<photostyle:0xb73dcb08 @image=#<StyleElement:0xb73dd058 @selector="#theimage", @style={"border"=>"0", "box-shadow"=>"1px 3px 15px #555"}>> 
</photostyle:0xb73dcb08>

I found above code snippet here. Thanks!

Read more about ActiveRecord's serialize method here: Simple user preferences for your Rails app.

Update:
Still not sure if that's a bug, but I submitted a lighthouse ticket.

Update 2:
Apparently this is not a bug in Rails, but a problem in my code :-) Please see Aaron's comment in above ticket.

DeliciousTwitterFacebookLinkedInRedditSlashdotTechnorati FavoritesDiggShare
Posted in Ruby, RubyOnRails | 2 Comments

JRuby and Rails on AppEngine

JRuby and Ioke on Google App Engine for Java

key topics:

  • Why it actually might be interesting to let your Ruby app run with Jruby instead of MRI: threading, Unicode, performance, memory (much better garbage collection), c extensions > Java extensions, use Java code directly from Ruby
  • Warbler: packages your Rails / Ruby application into a WAR ready to deploy
  • How get Ruby work on AppEngine
  • Testing: local testing problematic, functional tests need to run on AppEngine directly

All in all I got the feeling that you shouldn’t run your production Rails application on AppEngine yet. There are to many caveats currently. It might be worth considering to move specific services of your application to AppEngine though. Especially those which could benefit most from the scaling advantages in the cloud.

The main points for getting your Rails app running on AppEngine you can read here and here.

DeliciousTwitterFacebookLinkedInRedditSlashdotTechnorati FavoritesDiggShare
Posted in AppEngine, Ruby | Leave a comment

Google I/O und LA Ruby: Material für verregnete Wochenenden

Google Web Toolkit @ Google I/O
14 Videos from LA Ruby 2009: Some Great Weekend Watching

DeliciousTwitterFacebookLinkedInRedditSlashdotTechnorati FavoritesDiggShare
Posted in GWT, Ruby, RubyOnRails | Leave a comment