好记性不如铅笔头

编程

Ruby语法简单笔记

本文内容节选自张开川前辈的《RUBY语言入门教程 v1.0》,这里表示感谢,版权贵原作者所有。

#注释
#This is a comment
=begin
This is a comment
=end

#结尾分号与换行:
print "Hello World 1\n"; #可以加分号
print "Hello World 2\n"
print "Hello "\
"World 3\n";

#变量的命名规则
=begin
Ruby的标识名区分大小写。
Ruby使用一个约定来帮助它区别一个名字的用法:名字前面的第一个字符表明这个名字的用法。
局部变量、方法参数和方法名称应该用一个小写字母开头或者用一个下划线开头 
全局变量用美元符作为前缀 $
实例变量用 @ 开头
类变量用 @@ 开头
类名、模块名和常量应该用大写字母开头。
词首字母后面可以是字母、数字和下划线的任意组合; @ 后面不可以直接跟数字。
=end



#比较运算符
=begin
==  !=  比较两个对象的值是否相等
eql?    比较两个对象的值,类型是否相等
equal? 比较两个对象在内存中的地址是否相同
=end

#if语句
if i == 1 then
  print "Hello\n";
end

if i == 1
  print "Hello 1\n";
elsif i == 2
  print "Hello 2\n";
else
  print "Hello 3\n";
end

#unless语句
unless i == 1
  print "Hello\n";
end

#case语句
case i
  when 1
    print "Hello 1\n";
  when 2..4
    print "Hello 2\n";
  else
    print "Hello 3\n";
end

#while语句
while i < 3
  printf "Hello %d\n",i ;
  i = i + 1;
end

#unless语句
unless i > 3
  printf "Hello %d\n",i ;
  i = i + 1;
end

#for语句及简单数组
for i in 1...4
  print i ," ";
end
print "\n"
for i in 1..4
  print i ," ";
end
#输出
=begin
1 2 3 
1 2 3 4
=end

#循环控制语句
=begin
break ,跳出当层循环;
next ,忽略本次循环的剩余部分,开始下一次的循环;
redo ,重新开始循环,还是从这一次开始;
retry ,重头开始这个循环体。
=end

#简单循环语句
3.times { print "Hi!" } #Hi!Hi!Hi!
1.upto(9) {|i| print i if i<7 } #123456
9.downto(1){|i| print i if i<7 } #654321
(1..9).each {|i| print i if i<7} #123456
0.step(11,3) {|i| print i } #0369
	
	
#类的定义即使用
class Person #类名称大写
  def initialize(name, age = 18) #构造函数,含有默认参数
    @name = name; #实例变量使用 @ 
    @age = age;
  end
  
  def talk
    puts "Hello I am " + @name + "  I am " + @age.to_s;#int转str需要使用函数,一切皆是对象
  end
attr_writer:age;			#可写属性
attr_reader:name;			#可读属性
attr_accessor:des;		#读写属性
end

p1 = Person.new("XiaoMing", 18);
p1.talk;
p1.age = 20;
p1.talk;
puts "Name:" + p1.name;
p1.des = "123456";
puts "Des:" + p1.des;


#类的继承
class Student < Person #继承
  def talk #重写父类的方法
    puts "I am a student,my name:" + @name;
  end
end

p2 = Student.new("XiaoLi", 20);
p2.talk;

class StudentAdv < Student #继承
  def talk
    super(); #在子类中重新调用父类的方法
    puts "And I am StudentAdv";
  end
end


p2 = StudentAdv.new("XiaoLi", 20);
p2.talk;

#类变量和类方法的使用
class Counter
  @@count = 0; #类变量
  
  def initialize
    @@count += 1;
  end
  
  def Counter.showCnt #类方法
    puts "All Cnt:" + @@count.to_s;
  end
  
  def printCnt
    puts @@count.to_s;
  end

end

#类成员的访问控制
=begin
public     可以被任何实例对象调用,不存在访问控制;
protected  可以被定义它的类和其子类访问,可以在类中或子类中指定给实例对象;
private    可以被定义它的类和其子类访问,不能被实例对象调用。
方法默认都是公有的( initialize方法除外,它永远是私有的)。
=end

#模块
module MyMdl
	def showHello #普通方法
	  puts "Hello World"  
	end

	def MyMdl.showHello2 #模块方法
	  puts "Hello World2"  
	end
end

#直接调用模块方法
MyMdl.showHello2

#引入模块后,调用方法
include MyMdl
showHello
MyMdl.showHello

MyMdl.showHello2


#include 方法为一个类的所有对象包含某个模块 
#extend 方法为一个类的某个对象包含某个模块。
class Cnt2 < Counter
  include MyMdl
end

class Cnt3 < Counter
end

c3 = Cnt3.new;
c3.extend(MyMdl);



#require与load
=begin
require包含文件,只加载一次,遇到同一文件时自动忽略;不同路径下的同名文件会多次加载。 load包含文件,加载多次,即使是相同路径下同一文件。
require,load用于包含文件;include,extend则用于包含模块。
require加载文件时可以不加后缀名,load加载文件时必须加后缀名。
require一般情况下用于加载库文件,而load用于加载配置文件。
=end
require "xxx"
require "xxx.rb"
load "zz.rb"

 

发表评论

11 − 4 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据