Install Ruby(安装) For windows you can download Ruby from http://rubyforge.org/frs/?group_id=167 for Linux tryhttp://www.rpmfind.net. Our first program(从此开始) Enter the following into the file, "test.rb". puts "Howdy!" At the C: prompt en
编写自定义的迭代器 The defining feature of an iterator method is that it invokes a block of code associatedwith the method invocation. You do this with the yield statement. The followingmethod is a trivial iterator that just invokes its block twice:def twice
一.比较语句 大部分和其他的语言一样,这里注意<=>. 条件语句 如下几种形式 if if ..else.. end if..elsif..else..end unless(if not) case..when z 注意在ruby中只有nil和false为假. x=1 if x==1 puts 1 elsif x==2 puts 2 else puts 3 end 注意是elsif,不是elseif.与c相比,多了个end. 但Ruby的case语句和C/C++的格式差异很大: case 被判
ruby对象是严格封装的:只能通过定义的方法访问其内部状态.方法使用的成员变量在对象外部不能直接访问,不过可以通过getter.setter等访问器方法(accessor),使他们看起来好像是直接访问的. 与对象状态的封装性相反,ruby中的类非常开放.每个ruby程序都可以为现有类添加方法,而且也可以为单个对象添加“单键方法(singleton method)”. 创建类 Classes are created in Ruby with the class keyword:class Poin
赋值: ruby支持并行赋值,即允许在赋值表达式中出现多余一个值和多于一个的变量: x,y=1,2a,b=b,ax,y,z=[1,2,3] (python同样可以正常上面的语句). Methods in Ruby are allowed to return more than one value, and parallel assignmentis helpful in conjunction with such methods. For example:# Define a method to