This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#--- | |
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-- | |
# 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中 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'pathname' | |
get "/" do | |
dir = "./files/" | |
@links = Dir[dir+"*"].map { |file| | |
file_link(file) | |
}.join | |
erb :index | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#--- | |
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#--- | |
# 该程序是经典的文件操作程序。 | |
# 在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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#--- | |
# 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" | |
#--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#--- | |
# 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!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#--- | |
#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 |
OlderNewer