Ruby的装饰器模式实现
class Tree
def initialize
puts "Make a normal tree"
end
def decorate
puts "Make sure the tree won\'t fall"
end
end
class RedBalls < Tree
def initialize(tree)
@parent = tree;
end
def decorate
@parent.decorate
puts "Put on some red balls"
end
end
class BlueBalls < Tree
def initialize(tree)
@parent = tree;
end
def decorate
@parent.decorate
puts "Add blue balls"
end
end
class Angel < Tree
def initialize(tree)
@parent = tree;
end
def decorate
@parent.decorate
puts "An angel on the top"
end
end
tree = Angel.new(BlueBalls.new(RedBalls.new(Tree.new)))
tree.decorate
相关文档:
上一篇文章
说了Ruby的安装和运行,也简单的说了下类和对象。这里主要谈谈变量的问题。
先说常量
。如果变量名以大写字母开头,就被视为常量,但通常是所有字母都大写。但和其他语言不同,在Ruby中,你仍然可以改变常量的值,当然解释器会抛出一个警告:
#! /usr/bin/ruby
CONSTANT = 1
print "#{CONSTANT}\n&qu ......
订单处置,首先要有一个订单的详细列表(包括数量,价钱啥的)和一个对于个人的一些信息的一个表。因而我们创造两个模型,line_item(列表项),order(列表),其后编者如次
/db/migrate xxx_create_order xxx_line_item
Ruby代码
一.class CreateOrders < ActiveRecord::Migration
二.def self.up
3. create_tabl ......
ruby on rails安装mysql数据库
1. 下载mysql软件
http://www.mysql.cn/
mysql中文官方网站下载安装软件,选择5.0
2. 安装mysql,设置默认字符集为utf-8
3. 下载mysql for ruby的驱动并安装
http://rubyforge.org/搜索mysql,找到mysql -win
点击下载,进入页面
http://rubyforge.org/frs/?group_id= ......
require 'win32ole'
excel = WIN32OLE::new('excel.Application')
workbook = excel.Workbooks.Open('E:\RubyApp\bmk.xls')
worksheet = workbook.Worksheets(1) #get hold of the first worksheet
worksheet.Select #bring it to the front -need sometimes to run macros, not for working with a worksheet from ru ......
1.install ruby
download from http://www.ruby-lang.org/en/news/2009/12/07/ruby-1-9-1-p376-is-released/
2.install rails
ruby gem install rails --include-dependencies
3.install mysql
download from http://sourceforge.net
ruby gem install&nb ......