managing gems with rvm named gem sets

30/05/2010

RVM (Ruby Version Manager) is a tool which lets you install and switch between multiple ruby versions. RVM has also something called Named Gem Sets. This is pretty cool because you can create many different gem sets for different types of apps. Here is how to do it (I assume you have rvm already installed if not check this out):

  • go to your project folder and create new file called .rvmrc
  • open .rvmrc and add rvm ruby-version@your-gem-set for example rvm ruby-1.9.1@railsgems
  • close file and type: rvm gemset create your-gem-set (this will create new set)
  • type gem list (you should see empty list with no gems installed)

It’s almost as you would start with a fresh system.

No Comments

dfs in ruby :)

14/01/2010

I wrote simple dfs in ruby:

def dfs(node, value, queue)
  return false if node.nil?
  return true if node.data == value
 
  queue.push node.right unless node.right.nil?
  queue.push node.left unless node.left.nil
 
  dfs(queue.pop, value, queue) 
end

for node:

class Node
  attr_accessor :left, :right, :data
end
No Comments

Gemcutter & Jeweler

17/10/2009

More about gemcutter & jeweler can be found here.

Here are the steps how to publish patched gem cloned from github:

1. Append username to gem name in .gemspec or if you use jeweler open Rakefile and edit Jeweler::Tasks section save it and run:

rake gemspec

2. Build gem with:

gem build

3. Push new gem to gemcutter

gem push
No Comments

Cron in Ruby

27/07/2009

Check out how Whenever gem can simplify cron configuration.

No Comments

daemon_controller + Thinking Sphinx

22/07/2009

I’ve created simple rails initiator in order to start Sphinx through daemon_controller based on the Thinking Sphinx configuration. I hope it will help somebody.

require 'daemon_controller'
 
def before_start
  if not ThinkingSphinx.define_indexes?
    config = ThinkingSphinx::Configuration.instance
    cmd = "#{config.bin_path}#{config.indexer_binary_name} --config \"#{config.config_file}\" --all"
    cmd << " --rotate" if ThinkingSphinx.sphinx_running?
    system cmd
  end
end
 
if defined?(ThinkingSphinx)
  if not ThinkingSphinx.sphinx_running? 
    conf_instance = ThinkingSphinx::Configuration.instance
    @controller = DaemonController.new(
      :identifier => 'Sphinx search server',
      :start_command => "#{conf_instance.bin_path}#{conf_instance.searchd_binary_name} --pidfile --config \"#{conf_instance.config_file}\"",
      :before_start => method(:before_start),
      :ping_command => lambda { TCPSocket.new(conf_instance.configuration.searchd.address, conf_instance.configuration.searchd.port) },
      :pid_file => conf_instance.configuration.searchd.pid_file,
      :log_file => conf_instance.configuration.searchd.log)
      @controller.start
  end
end
4 Comments

ruby maxins in rails plugins

20/07/2009

Very often when looking at the code in rails plugins you can run into this:

module Taggable 
  def self.included(base)
    base.extend(ClassMethods)
  end
  module module ClassMethods
    #methods here
  end
end

This is a part of a bigger pattern which is shown below:

module ModuleA
  def self.included(base)
    # add class methods from ModuleB
    base.extend(ModuleB)
  end
end
 
module ModuleB
  def act_as_hello
    p "hello from module B"
  end
end
 
class ClassC
 #class body here
end
 
# include moduleA in classC
ClassC.send(:include, ModuleA)
 
class ClassD < ClassC
  act_as_hello
end
 
classD = ClassD.new

The pattern is used often when developing plugins with ActiveRecord. What we gain by inheriting from ClassC (class ClassD < ClassC) are instance methods from ModuleA. This is done by:

ClassC.send(:include, ModuleA)

Moreover since ModuleA is included in ClassC, ModuleA’s initializer def self.included(base) will be invoked at the time ModuleA is mixed with ClassC. The invocation will call base.extend(ModuleB). In this case base represents ClassC which will be extended by adding class methods from ModuleB. The ModuleA’s init method is shown again below:

def self.included(base)
  # add class methods from ModuleB to ClassC
  base.extend(ModuleB)
end

At the end our ClassD has now access to all class methods defined in ModuleB. act_as_hello will be called during ClassD initialization:

class ClassD < ClassC
  act_as_hello
end
No Comments

capistrano recipes for ubuntu

19/07/2009

Few nice capistrano recipes which may help you automate Ubuntu Server setup:

No Comments

Decoration in Ruby

30/06/2009

Interesting discussion about object decoration in ruby.

No Comments

few more ruby links

18/05/2009
No Comments

going back to ruby again…

4/04/2009

Here are few links which helped me understand few ruby concepts:

No Comments