示例1:
客户端输入加减乘除运算。
def arithmetic(num,operator,num2)
result=0
if
operator.eql?("+")
result = num+num2;
elsif
operator.eql?("-")
result = num-num2;
elsif
operator.eql?("*")
result= num*num2;
elsif
operator.eql?("/")
result=num/num2;
end
return result;
end
print "第一个数"
num = STDIN.gets #stdin =standard + input 标准输入用于从键盘中获取输入结果
num.chop!
print "运算符"
operator= STDIN.gets
operator.chop!
print "第二个数"
num2 = STDIN.gets #stdin =standard + input 标准输入用于从键盘中获取输入结果
num2.chop!
#result = arithmetic(8,"/",8)
#puts result;
=begin
键盘输入 都是字符串类型 to_i 把字符串转成数字类弄 to_s 把其他的转成字符串类型。
=end
result = arithmetic(num.to_i,operator.to_s,num2.to_i)
puts result
示例二
方法重写,在ruby 中没有方法重载,有的文章中说的是重载 其实 是方法重写。
class Animal
def initialize(name)
@name=name
end
def breathe name='pig'
speak # 调用内部方法
return "I am #{@name} 调用呼吸的方法"
end
def speak
puts "I am Animal"
end
def train_toll(age)
if age < 12
print "Reduced fare.\n";
else
print "Normal fare.\n";
end
end
private:speak; # 可以使用 private:mathodName 把方法设置成 内部 方法,外部可见 但不能使用
end
dog = Animal.new("dog")
puts dog.breathe;
class Cat < Animal #表示继承
def initialize(name)
@name=name
end
def speak
super # 使用 super 可以 加强父类方法
return "I am #{@name} 调用叫的方法"
end
def train_toll(age)
super(age) # 可以使用super 向父类传递参数
end
end
=begin
子类继承父类时,当创建子类时,先创建父类 ,当父类提供了构造方法 就不会 提供默认的构造方法
ruby 中没有方法重载 不能提供多个构造方法 或 方法
=end
cat = Cat.new("cat")
puts cat.speak
puts cat.breathe
cat.train_toll(20);
示例三
数组和HashMap 的使用
=begin
数组的基本用法
=end
a = [1,2.1,true,"abc",'122'] # 定义一个数组
puts a.length # 长度
puts a.size # 长度
puts a[1] # 下标为1 的值
puts a[0]="first" # 给数组a[0] 赋值
print a # ["first", 2.1, true, "abc", "122"]
b = Array.new; # 定义一个新的数组
puts b.length # 测试 长度
b[0]="1212" # 给新的数组赋值
puts b.length # 新数组的长度
puts b[0] # 获取下标为0 的新数组值
puts b[1]="22" # 获取下标为1 的新数组值 b[1212,22]
puts b[-1] # 从右到左取数 -1 为最右边的第一个数
puts b[-10] # 取一个不存在的数 会返回一个nil
print a[1..3] # [2.1, true, "abc"] 从下标为1取值取3个
print a[1...3] # [2.1, true] 从下标1 取值取2个 不包话最后一个
print a[1,3] # [2.1, true, "abc"] 从下标为1取值取3个
print a[-3,2] # [true, "abc"] 从右边第3个取值取2个
print a[-3..-1] # [true, "abc", "122"]从右边第3个取值 取到最后1个
print "\n----------------------------------------------------\n"
=begin
HashMap 的使用
=end
h = { 'dog' => 'canine', 'cat' => 'feline', 'donkey' => 'asinine', 'name' => 'zs' } # 定义一个haspMap 对象 h ,{'key' => 'value'}必须加上''
puts h.length # 获取 h 的长度
puts h['dog'] # 通过 key 获取key对应的值
puts h['cat']='Tom' # 赋值时key 相同就会把原HashMap 中的值覆盖
print h # {"dog"=>"canine", "cat"=>"Tom", "donkey"=>"asinine", "name"=>"zs"}
puts h["birgs"]="single birg"# 新增一个值
puts h #{"dog"=>"canine", "cat"=>"Tom", "donkey"=>"asinine", "name"=>"zs", "birgs"=>"single birg"}
puts h[12]="age" # 可以使用数字做为key
puts h # {"dog"=>"canine", "cat"=>"Tom", "donkey"=>"asinine", "name"=>"zs", "birgs"=>"single birg", 12=>"age"}
进一步剖析数组的使用
class SongList
def initialize
@songs = Array.new
end
def append(aSong)
@songs.push(aSong)
self
end
def deleteFirst
@songs.shift
end
def deleteLast
@songs.pop
end
def [](key) # 定义方法可以使有
if key.kind_of?(Integer)
return @songs[key]
else
for i in 0...@songs.length
#return @songs[i] if key == @songs[i].name # ruby 可以把判断条件写到返回的后边,当然 先判断再输出也是对的
return @songs.find{|aSong| key == aSong.name} # 和上边的功能一样
end
end
return nil
end
=begin 以下是上边方法的简写
def [](key)
return @songs[key] if key.kind_of?(Integer)
return @songs.find { |aSong| aSong.name == key }
end
=end
end
class Song
def initialize(name,singName,time)
@name=name;
@singName=singName
@time=time
end
attr_reader :name, :singName, :time # 提供对song中的每个属性的读取方法
attr_writer :name, :singName, :time # 提供对song中的每个属性的设置方法
def to_s
return "#@name #@singName #@time"
end
end
list = SongList.new
list.
append(Song.new('title1', 'artist1', 1)).
append(Song.new('title2', 'artist2', 2)).
append(Song.new('title3', 'artist3', 3)).
append(Song.new('title4', 'artist4', 4))
print list
print "\n---------------------------\n"
puts list.[](1) # title2 artist2 2 获取下标是1的元素
print list.[]('title1')
print "\n-----------------------------\n"
print (1..10).to_a # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print "\n"
songs = Array.new # 创建一个新的数组
songs.push("aSong")# 给一个数组里添加新的内容
songs.push("bSong")
print songs # ["aSong", "bSong"]
#puts songs.pop # bSong 从数组的最底下获移除元素
#puts songs # aSong
puts songs.shift # aSong 从数组的最上移除元素并返回此元素
puts songs # bSong