Skip to content

Instantly share code, notes, and snippets.

@louis-wu
louis-wu / rspec_tennis_scorer.rb
Created January 25, 2010 18:53
Ruby rspec测试:tennis scorer
#---
# Excerpted from "Programming Ruby", P210-214
# 运行测试:$ spec rspec_tennis_scorer.rb
#---
require "tennis_scorer"
describe TennisScorer, "basic scoring" do
before(:each) do
@ts = TennisScorer.new
end
@louis-wu
louis-wu / coderay.rb
Created January 27, 2010 15:38
使用coderay和railscasts样式进行代码高亮
#--
# CodeRay是一个语法高亮的Ruby库.支持的语法包括: Ruby, HTML, RHTML,YAML,SQL等。
# 官方文档: http://coderay.rubychan.de/doc/
# 以下示例来自于: http://hideto.javaeye.com/blog/204525
#--
# 1,安装coderay gem
gem install coderay
# 2,在application.rb中
@louis-wu
louis-wu / struct.rb
Created January 28, 2010 09:21
metaprogramming
#Struct allows you to define classes that contain just data attributes.
Person = Struct.new(:name, :address, :likes)
dave = Person.new('Dave', 'TX')
dave.likes = "Programming Languages"
puts dave
# opening up the class and writing the method
Person = Struct.new(:name, :address, :likes)
class Person
@louis-wu
louis-wu / butler.rb
Created April 20, 2010 04:14 — forked from bhb/butler.rb
sinatra and iui
require 'sinatra'
require 'pathname'
get "/" do
dir = "./files/"
@links = Dir[dir+"*"].map { |file|
file_link(file)
}.join
erb :index
end
@louis-wu
louis-wu / book_in_stock.rb
Created April 20, 2010 04:51
stock_stats
#---
# Our job is to take all the CSV files and work out how many of each title we have,
# as well as the total list price of the books in stock.
# CSV files looks something like this:
# "Date", "ISBN", "Amount"
# "20080412", "9781934356104", 39.45
# "20080413", "9781934356166", 45.67
# Excerpted from "Programming Ruby"
#---
class BookInStock
@louis-wu
louis-wu / exercise-differences.rb
Created April 20, 2010 04:57
inventory difference
#---
# 该程序是经典的文件操作程序。
# 在shell里运行:ruby exercise-difference.rb new-inventory.txt old-inventory.txt
#---
#检查运行本程序的参数是否设置正确
def check_usage # (1)
unless ARGV.length == 2
puts "Usage: differences.rb old-inventory new-inventory"
exit
@louis-wu
louis-wu / 20080729.xml
Created April 21, 2010 13:43
High-Performance XML Parsing
<?xml version='1.0'?>
<!--
! Excerpted from "Enterprise Recipes for Ruby and Rails"
! xml file containing credit card transaction.
! 应用的目录为: ccdemo/data/cc_xactions/20080729.xml
-->
<cc-xactions date='20080729'>
<cc-xaction id='100001' cc-ref='2537403' type='credit' amount='12.00'>
<text>Monthly bill.</text>
</cc-xaction>
@louis-wu
louis-wu / catalog.rb
Created April 21, 2010 15:06
Prototype Services with Sinatra
#---
# Use sinatra to build a prototype of a catalog service to manage a list of products
# The actions we’d like to simulate:
# HTTP Verb URI Action
# GET /products Returns a list of all products
# GET /products/:id Returns the product identified by :id
# POST /products Creates a new product
# DELETE /products/:id Deletes the product identified by :id
# Excerpted from "Enterprise Recipes for Ruby and Rails"
#---
@louis-wu
louis-wu / Proc.rb
Created May 17, 2010 08:58
Proc and Block
#---
# Ruby Facts: Proc and Block
#---
#take a block of code (code in between do and end),
#wrap it up in an object (called a proc),
# store it in a variable or pass it to a method,
#and run the code in the block whenever you feel like it
>> toast = Proc.new do
?> puts "cheers!"
#---
#YAML is a format for representing objects as strings.
#YAML is nice because it’s human-readable
#---
#YAML::dump: convert your Person object array into YAML data
>> require 'yaml'
=> true
>> class Person