2699425249_32de9cf0fc_o_normalcodemariner

http://twitter.com/codemariner

Proper backtrace with eval'd heredoc in Ruby

module MyModule
  module ClassMethods
    def string(name)
      module_eval <<-RUBY , __FILE__, __LINE__ + 1
        def #{name}
          raise "#{name}" #line number 6
        end
      RUBY
    end
  end

  def self.included(base)
    base.extend(ClassMethods)
  end
end


class MyClass
    include MyModule

    string :foo
end

m = MyClass.new
m.foo

# results in
# $ ruby ./test.rb 
#./test.rb:6:in `foo': foo (RuntimeError)
#	from ./test.rb:25
Reveal More
Added 9 months ago

SSH port forwarding with Ruby

#!/usr/bin/env ruby

require 'rubygems'
require 'net/ssh'

hosts = {:diginode2 => {:host => "64.93.11.111", :forwarding => [
                          {:local_port => 3002, :local_host => "localhost", :remote_port => 3000}, # admin webapp
                          {:local_port => 1936, :local_host => "localhost", :remote_port => 1935}, # fms rtmp
                          {:local_port => 1112, :local_host => "localhost", :remote_port => 1111} # fms admin
                         ]
         },
         :diginode1 => {:host => "64.93.11.112", :forwarding => [
                          {:local_port => 3000, :local_host => "192.168.19.50", :remote_port => 3000}, # admin app
                          {:local_port => 3001, :local_host => "localhost", :remote_port => 3001}, # admin app
                          {:local_port => 1935, :local_host => "localhost", :remote_port => 1935}, # fms rtmp
                          {:local_port => 1111, :local_host => "localhost", :remote_port => 1111} # fms admin
                         ]
         },
         :diginode3 => {:host => "64.93.11.113", :forwarding => [
                          {:local_port => 1937, :local_host => "localhost", :remote_port => 1935}, # fms rtmp
                          {:local_port => 1113, :local_host => "localhost", :remote_port => 1111} # fms admin
                         ]
         },
         :record1 => {:host => "64.93.11.211", :forwarding => [
                          {:local_port => 9001, :local_host => "localhost", :remote_port => 8000} # json rpc
                         ]
         },
         :record2 => {:host => "64.93.11.212", :forwarding => [
                          {:local_port => 9002, :local_host => "localhost", :remote_port => 8000} # json rpc
                         ]
         },
         :record14 => {:host => "64.93.11.224", :forwarding => [
                          {:local_port => 9014, :local_host => "localhost", :remote_port => 8000} # json rpc                                                                                                  
                         ]
        }
}




hosts.each do |name, config|
  Thread.new {
    puts "connecting to #{config[:host]}"
    Net::SSH.start(config[:host], "diginode", :password => "fc8arca#0ne") do |ssh|
      forwards = config[:forwarding]
      forwards.each do |f|
        puts "forwarding #{f[:local_port]}, #{f[:local_host]}, #{f[:remote_port]} from #{config[:host]}"
        ssh.forward.local(f[:local_port], f[:local_host], f[:remote_port])
      end
      ssh.loop {true}
    end
  }
end


while (true)
  sleep(0.5)
end 
Reveal More
Added 9 months ago

Rake task exception handler with Binding play

# something that represents a rake task                                                                          
class Task
  attr_accessor :name                                                                                                 
  def initialize(name)                                                                                                
    @name = name                                                                                                      
  end
end 
  
                                                                                                                      
# something like a rake 'task' method
def task(name, &block)
  yield Task.new(name)
end     
        
      
# a method that will execute a block defined inside of a rake task
# the idea is that this method can trap and report on exceptions
# I want to get the task name to include in the report in as transparent
# of a way as possible.  Unfortunately, it seems eval is the only way to
# go about getting this.
def handler(&block)
  begin
    yield
  rescue Exception => e
    # ideally, this would test for t first
    name = block.binding.eval('t.name')
    puts "error in task '#{name}': e.message"
  end
end


task "publish" do |t|
  handler do
    # stuff here
    # handler will pick up t
    raise "something bad happened"
  end
end


# this results in
#   error in task 'publish': e.message
Reveal More
Added 9 months ago

Builder-like configuration approach to set class variables on a config class.

module Stalky                                                                 
  # config object that will be globally accessible                                                                                                          
  # as Stalky::Config
  class Config
    # same ol attr_accessor but applied to the Config class itself
    class << self; attr_accessor :qwerly_api_key, :rapleaf_api_key; end

    def initialize(&block)
      # pass the Config class into the given block
      yield Config if block_given?
    end
  end
end


# put this in something like config/initializers/stalky.rb
Stalky::Config.new do |config|
  config.qwerly_api_key = "foo"
  config.rapleaf_api_key = "bar"
end


# displays 'foo'
puts Stalky::Config.qwerly_api_key
# displays 'bar'
puts Stalky::Config.rapleaf_api_key
Reveal More
Added 9 months ago

Re: Load yml configuration to a constant.

# stick this in some kind of initializer code like config/environment.rb
APP_CONFIG = YAML.load_file("#{Rails.root}/config/app_config.yml")

# to make it environment specific
APP_CONFIG = YAML.load_file("#{Rails.root}/config/app_config.yml")[Rails.env]

# sample yaml :
<<-YAML # put this in config/app_config.yml

development:
  api_key: abcdefg

production:
  api_key: 1234567

YAML


# then you can access like this:
api_key = APP_CONFIG['api_key']
Reveal More
Added 9 months ago

Load yml configuration to a constant.

# stick this in some kind of initializer code like config/environment.rb
APP_CONFIG = YAML.load_file("#{Rails.root}/config/app_config.yml")

# to make it environment specific
APP_CONFIG = YAML.load_file("#{Rails.root}/config/app_config.yml")[Rails.env]

# sample yaml :
<<-YAML # put this in config/app_config.yml

development:
  api_key: abcdefg

production:
  api_key: 1234567

YAML

# then you can access like this:
api_key = APP_CONFIG['api_key']
Reveal More
Added 9 months ago

Truncate string while respecting word boundaries

# truncates a string based on word boundaries rather than
# actual length
def truncate_words(text, num_words = 30, end_string = '...')
  return unless text
  words = text.split()
  words[0..(num_words-1)].join(' ') + (words.size > num_words ? end_string : '')
end
Reveal More
Added 11 months ago
Post Code