http://www.ruby-lang.org/
这里有第一版的部分中文翻译下载最新版本的Ruby,安装之后就有一个快捷指向ProgrammingRuby.chm,
很不错的书,已经有了2005年的2nd版本,可以到
这里下载。(顺便做个广告
)
按照上面的例子,建立两个文件,
test.rb
1 class Song
2 def initialize(name, artist, duration)
3 @name = name
4 @artist = artist
5 @duration = duration
6 end
7 def to_s
8 "Song: #{@name}--#{@artist} (#{@duration})"
9 end
10 end
11
12 class Song
13 attr_reader :name, :artist, :duration
14 end
15
16 class KaraokeSong < Song
17 attr_reader :lyrics
18 def initialize(name, artist, duration, lyrics)
19 super(name, artist, duration)
20 @lyrics = lyrics
21 end
22 def to_s
23 "Karaoke" + super + "\nlyrics: #{@lyrics}"
24 end
25 end
test2.rb
1 class Song
2 attr_writer :name, :artist, :duration
3 end
然后在控制台下输入:
1 C:\>ruby
2 load "d:/ruby/mycode/test.rb"
3 load "d:/ruby/mycode/test2.rb"
4 song = Song.new("Bicylops", "Fleck", 260)
5 puts song.inspect
6 puts song.to_s
7 song.name="Groovy"
8 song.artist="Java"
9 song.duration=15
10 puts song.inspect
11 puts song.to_s
12 ^D
13 #<Song:0x28366e0 @name="Bicylops", @duration=260, @artist="Fleck">
14 Song: Bicylops--Fleck (260)
15 #<Song:0x28366e0 @name="Groovy", @duration=15, @artist="Java">
16 Song: Groovy--Java (15)
注:这里的^D是Ctrl+D
这段代码,给我们什么启发呢?
posted on 2006-03-25 23:51
rox 阅读(237)
评论(0) 编辑 收藏 所属分类:
Ruby