eval_string.rb
class EvalString < String
def eval(b)
b.eval String.new(self)
end
end
irb session
>> foo = 1
=> 1
>> bar = 2
=> 2
>> str = EvalString.new "foo+bar"
=> "foo+bar"
>> str
=> "foo+bar"
>> str.eval(binding)
=> 3
class EvalString < String
def eval(b)
b.eval String.new(self)
end
end
>> foo = 1
=> 1
>> bar = 2
=> 2
>> str = EvalString.new "foo+bar"
=> "foo+bar"
>> str
=> "foo+bar"
>> str.eval(binding)
=> 3
require 'flexmock'
module GenericTestHelper
include FlexMock::TestCase
TIMESTAMP_MATCHER = '\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+]\d{2}:\d{2}'
def clear_mocked_method(mock, method)
return unless mock.is_a? FlexMock
mock.instance_eval { @expectations.delete method.to_sym }
end
def mock_method(mock, method, retval, with = nil)
mock = flexmock mock unless mock.is_a? FlexMock
clear_mocked_method mock, method
if with.nil?
mock.should_receive(method).and_return retval
else
mock.should_receive(method).with(with).and_return retval
end
end
end
require 'generic_test_helper'
class FooTest < Test::Unit::TestCase
include GenericTestHelper
def test_exceptions_are_logged
exception = nil
begin
1 / 0
rescue Exception => exception
end
mock_database_lookups
clear_mocked_method Bar.instance_eval{@flexmock_proxy}.mock, :find_by_id
flexmock(Bar).should_receive(:find_by_id).and_raise(exception)
assert_nothing_raised { Foo.run 0 }
assert_match /exception caught: #{Regexp.escape exception.message} #{exception.backtrace.map{|str| Regexp.escape(str)}.join "\n#{TIMESTAMP_MATCHER}" }/,
@stdout.string
end
end
: josh@josh; cat /etc/issue
Ubuntu 10.10 \n \l
: josh@josh; sudo aptitude install apache2 lxr
: josh@josh; sudo vim /etc/apache2/sites-available/default
: josh@josh; tail -6 /etc/apache2/sites-available/default
Alias /lxr /usr/share/lxr
Options All
AllowOverride All
: josh@josh; sudo vim /usr/share/lxr/http/.htaccess
: josh@josh; sudo cat /usr/share/lxr/http/.htaccess
SetHandler cgi-script
: josh@josh; sudo mkdir -p /usr/share/lxr/source/1.8.7-p334
: josh@josh; cd /usr/share/lxr/source/1.8.7-p334
: josh@josh; sudo tar xvjf /tmp/ruby-1.8.7.tar.bz2
: josh@josh; sudo mv ruby-1.8.7 ruby
: josh@josh; sudo vim /usr/share/lxr/source/versions
: josh@josh; sudo cat /usr/share/lxr/source/versions
1.8.7-p334
: josh@josh; sudo ln -s /usr/share/lxr/source/1.8.7-p334 /usr/share/lxr/source/defversion
: josh@josh; sudo genxref ruby
Starting pass 1: Collect identifier definitions.
(Pass 1) vms/vmsruby_private.c (993), file 1 of 262…
[...]
(Pass 3) identifier 8200 of maximum 17136…
(Pass 3) identifier 8300 of maximum 17136…
Completed pass 3 (0s):Information on 8316 identifiers dumped to disk.
: josh@josh; sudo chmod -R o+r .
: josh@josh; sudo vim /usr/share/lxr/http/lxr.conf
: josh@josh; sudo cat /usr/share/lxr/http/lxr.conf
# Configuration file.
# [...]
# The source is here.
sourceroot: /usr/share/lxr/source/$v/ruby/
srcrootname: Ruby
# [...]
module CodeTimerHelper
# Creates a CodeTimer section around the method
#
def timed_method(timer, method_name, section_name = method_name.to_s)
eigenclass = (class << self; self; end)
eigenclass.class_eval do
define_method method_name do |*args, &block|
timer.start(section_name)
begin
super(*args, &block)
ensure
timer.stop(section_name)
end
end
end
end
end
# See: http://thinkingdigitally.com/archive/capturing-output-from-puts-in-ruby/
require 'stringio'
module Kernel
def capture_stdout(out = StringIO.new)
begin
$stdout = out
yield
ensure
$stdout = STDOUT
end
out
end
def use_my_stdin(instream)
begin
$stdin = instream
yield
ensure
$stdin = STDIN
end
instream
end
end
require 'lib/redirect_stdio'
require 'lib/converter.rb'
class ConverterTest < Test::Unit::TestCase
def test_convert
input_data = [
[ # headers
'rank', # index 0
'', # index 1
'', # index 2
'', # index 3
'', # index 4
'', # index 5
'name', # index 6
'category', # index 7
],
[ # data line 0
'123', # index 0
'', # index 1
'', # index 2
'', # index 3
'', # index 4
'', # index 5
'Foo', # index 6
'Bar', # index 7
],
]
converter = Converter.new
instream = StringIO.new input_data.map{|line| line.join("\t")}.join("\n")
output = capture_stdout do
use_my_stdin(instream) { converter.convert }
end
input_data[1..-1].each do |data|
assert_match /^#{data[6]} is rank #{data[0]} in #{data[7]}$/, output.string
end
end
end
At a news conference Saturday night, a clearly emotional Dupnik, who has been close to both Giffords and Roll, repeatedly cited what he characterized as the "vitriol" that has infected political discourse. He said that his own state has become "the mecca for prejudice and bigotry."And here's a treasure trove of absurdity from The Huffington Post:
There is reason to believe, he said, that the shooting suspect "may have a mental issue," adding that people like that "are especially susceptible to vitriol."
Giffords expressed similar concern, even before the shooting. In an interview after her office was vandalized, she referred to the animosity against her by conservatives, including Sarah Palin's decision to list Giffords' seat as one of the top "targets" in the midterm elections.
"For example, we're on Sarah Palin's targeted list, but the thing is, that the way that she has it depicted has the crosshairs of a gun sight over our district. When people do that, they have to realize that there are consequences to that action," Giffords said in an interview with MSNBC.
During his campaign effort to unseat Giffords in November, Republican challenger Jesse Kelly held fundraisers where he urged supporters to help remove Giffords from office by joining him to shoot a fully loaded M-16 rifle. Kelly is a former Marine who served in Iraq and was pictured on his website in military gear holding his automatic weapon and promoting the event.
"I don't see the connection," between the fundraisers featuring weapons and Saturday's shooting, said John Ellinwood, Kelly's spokesman. "I don't know this person, we cannot find any records that he was associated with the campaign in any way. I just don't see the connection.
"Arizona is a state where people are firearms owners - this was just a deranged individual."
Law enforcement officials said members of Congress reported 42 cases of threats or violence in the first three months of 2010, nearly three times the 15 cases reported during the same period a year earlier. Nearly all dealt with the health care bill, and Giffords was among the targets.
#!/usr/bin/env ruby1.9.1
require 'money'
require 'test/unit'
class TestMoney < MiniTest::Unit::TestCase
def test_multiplication
five = Money.dollar(5)
assert_equal(Money.dollar(10), five * 2)
assert_equal(Money.dollar(15), five * 3)
end
def test_equality
assert(Money.dollar(5) == Money.dollar(5))
refute(Money.dollar(5) == Money.dollar(6))
refute(Money.dollar(5) == Money.franc(5))
end
def test_currency
assert_equal(:USD, Money.dollar(1).currency)
assert_equal(:CHF, Money.franc(1).currency)
end
def test_simple_addition
five = Money.dollar(5)
sum = five + five
bank = Bank.new
reduced = bank.reduce(sum, :USD)
assert_equal(Money.dollar(10), reduced)
end
def test_plus_returns_sum
five = Money.dollar(5)
sum = five + five
assert_equal(five, sum.augend)
assert_equal(five, sum.addend)
end
def test_reduce_sum
sum = Sum.new(Money.dollar(3), Money.dollar(4))
bank = Bank.new
result = bank.reduce(sum, :USD)
assert_equal(Money.dollar(7), result)
end
def test_reduce_money
bank = Bank.new
result = bank.reduce(Money.dollar(1), :USD)
assert_equal(Money.dollar(1), result)
end
def test_reduce_money_different_currency
bank = Bank.new
bank.add_rate(:CHF, :USD, 2)
result = bank.reduce(Money.franc(2), :USD)
assert_equal(Money.dollar(1), result)
end
def test_identity_rate
assert_equal(1, Bank.new.rate(:USD, :USD))
end
def test_mixed_addition
five_bucks = Money.dollar(5)
ten_francs = Money.franc(10)
bank = Bank.new
bank.add_rate(:CHF, :USD, 2)
result = bank.reduce(five_bucks + ten_francs, :USD)
assert_equal(Money.dollar(10), result)
end
def test_sum_plus_money
five_bucks = Money.dollar(5)
ten_francs = Money.franc(10)
bank = Bank.new
bank.add_rate(:CHF, :USD, 2)
sum = Sum.new(five_bucks, ten_francs) + five_bucks
result = bank.reduce(sum, :USD)
assert_equal(Money.dollar(15), result)
end
def test_sum_times
five_bucks = Money.dollar(5)
ten_francs = Money.franc(10)
bank = Bank.new
bank.add_rate(:CHF, :USD, 2)
sum = Sum.new(five_bucks, ten_francs) * 2
result = bank.reduce(sum, :USD)
assert_equal(Money.dollar(20), result)
end
end
class Money
attr_reader :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def self.dollar(amount)
Money.new(amount, :USD)
end
def self.franc(amount)
Money.new(amount, :CHF)
end
def ==(other)
@amount == other.amount &&
@currency == other.currency
end
def *(multiplier)
Money.new(@amount * multiplier, @currency)
end
def +(addend)
Sum.new(self, addend)
end
def reduce(bank, to_currency)
rate = bank.rate(@currency, to_currency)
Money.new(@amount / rate, to_currency)
end
end
class Bank
def initialize
@rates = {}
end
def reduce(expression, to_currency)
expression.reduce(self, to_currency)
end
def add_rate(from, to, rate)
@rates[[from, to]] = rate
end
def rate(from, to)
return 1 if from == to
@rates[[from, to]]
end
end
class Expression
end
class Sum
attr_reader :augend, :addend
def initialize(augend, addend)
@augend = augend
@addend = addend
end
def reduce(bank, to_currency)
amount = @augend.reduce(bank, to_currency).amount +
@addend.reduce(bank, to_currency).amount
Money.new(amount, to_currency)
end
def +(addend)
Sum.new(self, addend)
end
def *(multiplier)
Sum.new(@augend * multiplier, @addend * multiplier)
end
end
CRAIGAVON, Northern Ireland — Barely three days after two British soldiers were shot to death by a dissident faction of the Irish Republican Army, another I.R.A. breakaway group claimed responsibility on Tuesday for an ambush that killed a police officer on Monday night in this town 25 miles southwest of Belfast.
Politicians on both sides of the divide between the Protestant and Roman Catholic communities in the province denounced the killing as the work of “terrorists” [...]
The reaction in Craigavon [...] suggested that for now, [concerns that the peace might be destroyed] might be overblown. The town, in the county of Armagh, a major sectarian battlefield in the past, has a mixed population of Catholics and Protestants, but people approached at random at the site of the shooting and in shopping malls seemed confident that the peace would survive the shootings.
A 25-year-old Catholic man who gave his name only as William said that he supported Sinn Fein, the main nationalist party in the power-sharing government, and that he “feared something like this was going to happen” when I.R.A. sentiment split over the peace agreement. “There is still a small minority out there who agree with shootings and all that,” he said. “I don’t agree with it myself. There was a time when I may have agreed with it. But it is the wrong time. Violence is in the past.”


Felt by most people in the building. Some people are frightened. Dishes in a cupboard rattle occasionally. Electric wires swing slightly.
The Nos. 2, 3, 4 and 7 reactors at the Kashiwazaki-Kariwa nuclear power station in the prefecture automatically shut down after the earthquake, according to Tokyo Electric Power Co. A small fire broke out in an electricity transformer of the No. 3 reactor, but no damage was reported to the reactor. The fire was extinguished at about noon.
The Fiscal Year to Date deficit ($80 billion) is down 33 percent ($39 billion) compared to the same period last year. The President's tax relief has stimulated strong economic growth. This strong growth has contributed to record-level receipts and the creation of more than 7.2 million jobs since August 2003. October to December receipts for FY 07 are at $574 billion, running 8 percent ($43 billion) higher compared to the same period for FY 06.