Tatva-Artha

meaning of "it"

Archive for the ‘ruby’ tag

Installing RMagick gem on mac osx (Mountain Lion)

without comments

Several things have changed since the last time I wrote about gotchas when installing rmagick on mac osx.

Now a days, you need Homebrew instead of Port.

Install ImageMagick using homebrew first

$ brew install imagemagick

Before, you install rmagick gem though, find out the homebrew folder where imagemagick libraries are installed.

$ Magick-config --prefix

Change directory into this directory.

$ cd `Magick-config --prefix`

Now, symlink the versioned library to what the gem is looking for.

$ ln -s libMagick++-6.Q16.dylib libMagick++.dylib
$ ln -s libMagickCore-6.Q16.dylib libMagickCore.dylib
$ ln -s libMagickWand-6.Q16.dylib libMagickWand.dylib

Your specific versioned library may differ a little bit.

Now, you should be able to infall gem just fine.

$ gem install rmagick

Written by Sharad

October 6th, 2013 at 11:53 pm

Posted in All,Technology

Tagged with , ,

Intalling pg gem on ubuntu

without comments

Installing pg gem (for protgres) on ubuntu server requires following packages

$ sudo apt-get install libpq-dev
$ sudo apt-get install postgresql-server-dev-all

Written by Sharad

September 18th, 2012 at 2:20 pm

Posted in All

Tagged with , , ,

Upgrading to ruby 1.9: rbx-require-relative requires Ruby version ~> 1.8.7

without comments

If you are upgrading from ruby 1.8 or Ruby Enterprise 1.8.7 to ruby 1.9.2, you may encounter this error.

Installing rbx-require-relative (0.0.5) Unfortunately, a fatal error has occurred.
Please report this error to the Bundler issue tracker at https://github.com/carlhuda/
bundler/issues so that we can fix it. Thanks!/Users/sjain/.rvm/rubies/ruby-1.9.2-p290/
lib/ruby/site_ruby/1.9.1/rubygems/ installer.rb:364:in `ensure_required_ruby_version_met':
rbx-require-relative requires Ruby version ~> 1.8.7. (Gem::InstallError)

This most likely happens because you are declaring a dependency on ruby-debug gem in your Gemfile.

group :development do
  gem 'ruby-debug'
end

With ruby 1.9, you need to update this with new gem name ruby-debug19.

group :development do
  gem 'ruby-debug19'
end

This will eliminate the dependency on rbx-require-relative and fix the issue.

Written by Sharad

August 21st, 2011 at 1:55 pm

Posted in All,Technology

Tagged with , , ,

Debugging Passenger Memory Issues

without comments

There was an interesting thread on ruby-passenger application server newsgroup recently: http://groups.google.com/group/phusion-passenger/browse_thread/thread/f48ad0eb018a2482

Here is my take away:

  # returns [class, count] tuples for all active records in the heap 
  def ar_space 
    GC.start 
    h = Hash.new(0) 
    ObjectSpace.each_object do |o| 
      next if o.__id__ == self.__id__ 
      next unless ActiveRecord::Base === o 
      h[o.class.to_s] += 1 
    end 
    return h.sort{|a,b| -(a[1]<=>b[1])} 
  end

Nifty little snippet to fetch number of active record objects on heap. I am guessing, incorporating this as Controller after_filter can help with cost of each request..

Written by Sharad

May 1st, 2011 at 1:05 pm

Posted in All

Tagged with , ,

Deploy at will!

without comments

Shamelessly copying one point from a github blog. It is that good.

Deploy at Will!

At the first RailsConf I had the pleasure of hearing Martin Fowler deliver an amazing keynote. He made some apt metaphors regarding agile development that I will now paraphrase and mangle.

Imagine you’re tasked with building a computer controlled gun that can accurately hit a target about 50 meters distant. That is the only requirement. One way to do this is to build a complex machine that measures every possible variable (wind, elevation, temperature, etc.) before the shot and then takes aim and shoots. Another approach is to build a simple machine that fires rapidly and can detect where each shot hits. It then uses this information to adjust the aim of the next shot, quickly homing in on the target a little at a time.

The difference between these two approaches is to realize that bullets are cheap. By the time the former group has perfected their wind detection instrument, you’ll have finished your simple weapon and already hit the target.

In the world of web development, the target is your ideal offering, the bullets are your site deploys, and your customers provide the feedback mechanism. The first year of a web offering is a magical one. Your customers are most likely early adopters and love to see new features roll out every few weeks. If this results in a little bit of downtime, they’ll easily forgive you, as long as those features are sweet. In the early days of GitHub, we’d deploy up to ten times in one afternoon, always inching closer to that target.

Make good use of that first year, because once the big important customers start rolling in, you have to be a lot more careful about hitting one of them with a stray bullet. Later in the game, downtime and botched deploys are money lost and you have to rely more on building instruments to predict where you should aim.

Written by Sharad

April 6th, 2011 at 9:03 pm

Posted in All

Tagged with , , ,

Mocha unexpected invocation errors

without comments

If you find that your tests pass when run alone but fail with “unexpected invocation” inside “rake test”, you are most likely seeing behavior that is caused by mocha load order.

This is a documented gotcha with mocha. And also discussed here.

It happens when mocha gem is listed as library dependency inside rails app. The library gets loaded too soon. The fix is to load mocha after rails has booted up.

For rails < 3.0.0, this means removing -- config.gem 'mocha' -- entry from config/environment.rb and adding -- require 'mocha' -- at the bottom of test/test_helper.rb.

For rails >= 3.0.0, this means adding — :require => false — inside Gemfile entry as follows.

# Gemfile
group :test do
  gem 'mocha', :require => false
end

And then adding following line at the bottom of test/test_helper.rb:

# test_helper.rb
require 'mocha'

Written by Sharad

March 11th, 2011 at 10:38 pm

Posted in All,Technology

Tagged with , ,

Installing RMagick gem on Mac OSX

without comments

Commands for installing image-magick package and rmagick gem on max osx:

sudo port install tiff -macosx imagemagick +q8 +gs +wmf
sudo gem install rmagick

Reference: http://rmagick.rubyforge.org/install-osx.html

Written by Sharad

March 8th, 2011 at 10:41 pm

Posted in All,Technology

Tagged with , ,

ActiveRecord::MissingAttributeError: missing attribute — a bug or a feature?

with 4 comments

Sooner or later, the need arises to assign default values to model attributes. In rails this is usually done in model’s after_initialize() with a new_record? guard.

Something like:

class User
  def after_initialize
    if new_record?
      self.country = "US"
    end
  end
end

This initializes the object with proper default so the UI form gets properly populated and the database gets proper default if user hasn’t overridden it.

Sometimes, you realize that you have (pre-existing?) objects in database that also need the same default if the current value is nil. In such case, you may modify your initializer like this:

class User
  def after_initialize
    self.country ||= "US"
  end
end

This works for both scenarios: User.new and User.find.

However, it introduces a behavior that may not be apparant immediately.

$ script/console
ree> User.find(u.id)
  User Load (0.9ms)   SELECT * FROM `users` WHERE (`users`.`id` = 1) 
ree> User.exists?(:id => u.id)
  RateSearch Load (0.6ms)   SELECT `users`.id FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
ActiveRecord::MissingAttributeError: missing attribute: country
/[prj]/app/models/User.rb:3:in `after_initialize'

A closer look reveals that ActiveRecord tries to be smart and only fetch ‘id’ column when performing Model.exists? call.

Many people have tripped this and logged it as a bug. The report has been silently ignored, and I believe, for good/performance reasons.

So, what do we get around it? Here’s one way.

class User
  def after_initialize
    self.country ||= "US"
  rescue ActiveRecord::MissingAttributeError
    # this should only happen on Model.exists?() call. It can be safely ignored.
  end
end

As they say, it is not a bug, it is a feature.

Written by Sharad

March 3rd, 2011 at 8:07 pm

Posted in All,Technology

Tagged with , ,

Online Regular Expression Tool

without comments

Rubular, is a tool that you may find handy when developing ruby regular expression for scraping unstructured data needs.

http://rubular.com/

Good one!

Written by Sharad

March 2nd, 2011 at 6:14 pm

Installing mysql gem with bundler on Snow Leopard

with 2 comments

Between 0.9.26, RC and final 1.0 release, bundler went thru some heavy changes with respect to command line options it supports. Luckily, twitter, forum and blogs kept everybody in the loop.

The blogs, however, became obsolete quickly and even blog posts few months old don’t work with latest version of bundler. We came across this issue that kept is in a loop for a while.

Snow Leopard changed a few things with ruby and mysql gem when it came out. Most mysql installation issues are hammered out by now and are well documented.

Read the rest of this entry »

Written by Sharad

October 7th, 2010 at 6:34 pm

Posted in All

Tagged with , , , , ,