#第十二课 逻辑运算a,b = true, falseputs a && b #逻辑与运算puts a || b #逻辑或运算puts !a #取反puts !bputs c = (4 >3)puts (4 > 3) && b#三元运算a, b = 4,5puts c = (a > b) ? a : b#范围运算(2..5).each do |n|#从2到5闭区间 print nend(2...5).each do |n|#从2到4左闭右开 print nend#Ruby中符号也可以是方法名class Student Version = 2#常量 @@all_num = 2 #全局变量得要初始化不然要报错 include Math #导入Mathm模块 Mix-in extend Math def initialize(name, num, sex) @name = name @num = num @sex = sex end class << self def class_fun puts "这是一个类方法" end def class_fun2 puts "这也是一个类方法" end end def self.class_fun3 puts "这是类方法的另外一种写法" end #相当于Java中的get方法 def get_name @name puts @@all_num += 1 end def name @name end #相当于Java中的set方法 def name=(value) @name = value end def set_name(name) @name = name puts @@all_num += 1 endendclass Student def expend puts "扩展类" endendclass MiddleStudent < Student attr_reader :name attr_writer :name def initialize(name) @name = name end def sub_fun puts "这是子类的方法#{@name}" endendputs s = Student.new("young", 20, "男")puts s.get_nameputs s.nameputs s.set_name("改了个名字")puts s.name =("改了个好名字")m = MiddleStudent.new("初中生")puts m.nameclass Vector attr_accessor :x, :y def initialize(x, y) @x = x @y = y end def +(value) #符号+做为方法名 Vector.new(@x + value.x, @y + value.y) end def -(value)#符号-做为方法名 Vector.new(@x - value.x, @y - value.y) endendp a = Vector.new(3, 5)p b = Vector.new(6, 9) p a + bp a - b