ruby基础学习二

上一篇 / 下一篇  2012-12-14 13:24:07 / 个人分类:程序设计

=begin
#父类子类
class Pet
  attr_accessor :name,:color,:age
  def eat
  end
end
class Snake < Pet
  attr_accessor :length
  def eat
    puts "frog"
  end
end
# instance for three class
 ins_snake = Snake.new
# instance for snake variable
 ins_snake.name = "zara"
 ins_snake.color = "black"
 ins_snake.length = 2
 ins_snake.age = 2
 #print the variables and methods of the class
 puts ins_snake.age
 puts ins_snake.eat
=end

=begin
#原样输出
x = <<ends_of_string
hello
hello,ruby
ends_of_string
puts x
=end

=begin
#正则表达式
p 120.chr
#全局搜索
puts "thos os a test".gsub('o','i')
#行开头两字符
puts "this is a test".gsub(/^../,'hello')
#行末尾两字符
puts "this is a test".gsub(/..$/,' hello')
#单个字符扫描
"123".scan(/./){|char| puts char}
a =[1,2,3,"yy"]
p a.collect {|elm| elm * 2}
p a[0..3]
=end

=begin
#空数组
x = []
p "x is empty" if x.empty?
p x.include?("x")
y = [2,3]
p y.first
p y.last
p y.first(2).join("-")
p y.reverse().inspect
=end

TAG:

 

评分:0

我来说两句

Open Toolbar