Tag Archive for ruby

BeforeAndAfter

module BeforeAndAfter
# This extends the class that includes BeforeAndAfter with the methods in ClassMethods
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods
def use_method *methods
methods.each { |method|
# Set up the before and after variables
before_method = "before_#{method.to_s}".to_sym
after_method = "after_#{method.to_s}".to_sym
# Unbind the original, before, and after methods
unbinded_before_method = instance_method( before_method )
unbinded_method = instance_method( method )
unbinded_after_method = instance_method( after_method )
# Define the before and after methods if they don't already exist
define_method( before_method ) unless self.method_defined?( before_method )
define_method( after_method ) unless self.method_defined?( after_method )
# Redefines the method to run the before and after methods
define_method( method ) {
unbinded_before_method.bind( self ).call # Bind the unbinded BEFORE method
unbinded_method.bind( self ).call        # Bind the original method
unbinded_after_method.bind( self ).call  # Bind the unbinded AFTER method
}
}
end
end
end

source

Freeze a single Gem in Rails

rake gems:unpack GEM=coderay

source

Ruby LRU Cache

class LRUCache

def initialize(size = 10)
@size = size
@store = {}
@lru = []
end

def set(key, value)
@store[key] = value
set_lru(key)
@store.delete(@lru.pop) if @lru.size > @size
end

def get(key)
set_lru(key)
@store[key]
end

private
def set_lru(key)
@lru.unshift(@lru.delete(key) || key)
end
end

if __FILE__ == $0
require 'test/unit'

class TestLRUCache < Test::Unit::TestCase

def setup
@cache = LRUCache.new(2)
end

def test_last_droped
@cache.set(:a, 'a')
@cache.set(:b, 'b')
@cache.set(:c, 'c')

assert_nil @cache.get(:a)
assert_equal 'b', @cache.get(:b)
assert_equal 'c', @cache.get(:c)
end

def test_get_keeps_key
@cache.set(:a, 'a')
@cache.set(:b, 'b')
@cache.get(:a)
@cache.set(:c, 'c')

assert_equal 'a', @cache.get(:a)
assert_nil @cache.get(:b)
assert_equal 'c', @cache.get(:c)
end

def test_set_keeps_key
@cache.set(:a, 'a')
@cache.set(:b, 'b')
@cache.set(:a, 'a')
@cache.set(:c, 'c')

assert_equal 'a', @cache.get(:a)
assert_nil @cache.get(:b)
assert_equal 'c', @cache.get(:c)
end
end
end

source

Command + Document Responsibility Separation Proposal

module DocumentFunctions
module InstanceMethods
# returns the word that is currently touched by the cursor
def word_touching_cursor

end
end

def self.included(receiver)
receiver.send :include, InstanceMethods
end
end

module DocumentManipulator

module InstanceMethods
def document

end

# replace a range with the given text.
def replace_range(start_offset, stop_offset, text)

end
end

def self.included(receiver)
receiver.send :include, InstanceMethods
end

end

module SelectionManipulator
include DocumentFunctions

module InstanceMethods

def selection

end
end

def self.included(receiver)
receiver.send :include, InstanceMethods
end

end

class FooCommand
include SelectionManipulator

def run
(self.methods - Object.new.methods).each do |method|
puts method
end
end
end

FooCommand.new.run

source

Waiting for events with the Selenium-Client Ruby driver

@selenium.wait_for_element "//div[@id='foo']"

# if the application under test uses jQuery, then you can do

@selenium.wait_for_condition "$('#foo').css('display') == 'block'", 2

# otoh if jQuery is not available, use native DOM methods:

@selenium.wait_for_condition "document.getElementById('foo').style.display == 'block'", 2

# Note that, within wait_for_condition, I did NOT need to use
# this.browserbot.getCurrentWindow().$() and instead I could
# simply refer to $()

source

Update older gem to now

sudo gem install rubygems-update
sudo update_rubygems

source

Be careful with immediate values

# immediate values
# e.g. true, false and Integers
a = true # => true
b = true # => true

def a.foo
"bar"
end

a.foo # => "bar"
b.foo # => "bar"

# referenced values
c = "foo" # => "foo"
d = "foo" # => "foo"

def c.meep
"blubb"
end

d.meep # => undefined method

source

generate Z random string

(0..36).map{65.+(rand(25)).chr}.join

source

generate Y random string

(0..36).map{65.+(rand(25)).chr}.join

source

generate X random string

(0..36).map{65.+(rand(25)).chr}.join

source