#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
#coding=utf-8
puts "This is main Ruby Program"
puts "http://www.runoob.com/ruby/ruby-encoding.html"
puts __FILE__
puts __LINE__

#我是注释
END {
  puts "声明 code 会在程序的结尾被调用。"
  puts "Terminating Ruby Program"
}
=begin
http://www.runoob.com/ruby/ruby-encoding.html
  若包含中文编码,则需要注意两点:
1. 必须在首行添加 # -*- coding: UTF-8 -*-,告诉解释器使用utf-8来解析源码。
2. 必须设置编辑器保存文件的编码为utf-8。
=end

BEGIN {
  puts "声明 code 会在程序运行之前被调用"
  puts "Initializing Ruby Program"
}

datatype.rb

#!/usr/bin/ruby
#coding=utf-8

#整型 Integer 以下是一些整型字面量
#字面量(literal):代码中能见到的值,数值,bool值,字符串等都叫字面量
#如以下的0,1_000_000,0xa等
a1=0
#带千分符的整型
a2=1_000_000
#其它进制的表示
a3=0xa
puts a1,a2
puts a3
#puts print 都是向控制台打印字符,其中puts带回车换行符
=begin
这是注释,称作:嵌入式文档注释
类似C#中的/**/
=end

#浮点型
f1=0.0
f2=2.1
f3=1000000.1
puts f3

puts 2**(1/4) #1与4的商为0,然后2的0次方为1
puts 16**(1/4.0)  #1与4.0的商为0.25(四分之一),然后开四次方根


puts 'escape using "\\"';
puts 'That\'s right';


puts "Multiplication Value : #{24*60*60}";#使用序列 #{ expr } 替换任意 Ruby 表达式的值为一个字符串。在这里,expr 可以是任意的 Ruby 表达式。

name="Ruby"
puts name
puts "#{name+",ok"}"

哈希和数组

#!/usr/bin/ruby
#coding=utf-8
puts "数组类型";

ary = [ "fred", 10, 3.14, "This is a string", "last element", ]
ary.each do |i|
  puts i
end

names=Array.new(20)
puts "#{names}"
puts names.size  # 返回 20
puts names.length # 返回 20

names=Array.new(4,"mac")
puts "#{names}"

nums=Array.new(10){ |e| e= e*2}
puts "#{nums}"

puts ("数组初始化")
nums = Array.[](1, 2, 3, 4,5)
nums2 = Array[1, 2, 3, 4,5]
digits = Array(0..9)
puts "#{nums}"
puts "#{nums2}"
puts "#{digits}"
num = digits.at(6)
puts "#{num}"

a = [ "a", "b", "c" ]
n = [ 65, 66, 67 ]
#压缩指令
puts a.pack("A3A3A3")   #=> "a  b  c  "
puts a.pack("a3a3a3")   #=> "a\000\000b\000\000c\000\000"
puts n.pack("ccc")      #=> "ABC"


print  "哈希键值对\n"

hsh = colors = { "red" => 0xf00, "green" => 0x0f0, "blue" => 0x00f }
hsh.each do |key,value|
  print key," is ",value,"\n"
end

months = Hash.new( "month" )
puts "#{months[0]}"
puts "#{months[72]}"

H = Hash["a" => 100, "b" => 200]
puts "#{H['a']}"
puts "#{H['b']}"

months = {"1" => "January", "2" => "February"}
keys = months.keys
puts "#{keys}"

print "范围类型\n";
(5..25).each do |n|
  print n," "
end



类和对象

#!/usr/bin/ruby
#coding=utf-8
class Customer
=begin
局部变量:局部变量是在方法中定义的变量。局部变量在方法外是不可用的。在后续的章节中,您将看到有关方法的更多细节。局部变量以小写字母或 _ 开始。
实例变量:实例变量可以跨任何特定的实例或对象中的方法使用。这意味着,实例变量可以从对象到对象的改变。实例变量在变量名之前放置符号(@)。
类变量:类变量可以跨不同的对象使用。类变量属于类,且是类的一个属性。类变量在变量名之前放置符号(@@)。
全局变量:类变量不能跨类使用。如果您想要有一个可以跨类使用的变量,您需要定义全局变量。全局变量总是以美元符号($)开始。
=end
  @@no_of_customers=0
  def initialize(id, name, addr)
    @cust_id=id
    @cust_name=name
    @cust_addr=addr
  end
  def display_details()
    puts "Customer id #@cust_id"
    puts "Customer name #@cust_name"
    puts "Customer address #@cust_addr"
  end
  def total_no_of_customers()
    @@no_of_customers += 1
    puts "Total number of customers: #@@no_of_customers"
  end
  def to_s
    "Customer:#@cust_id--#@cust_name--(#@cust_addr)"
  end
end
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")
puts cust1.to_s
puts cust2.to_s

puts cust1.display_details
puts cust1.total_no_of_customers
puts cust1.total_no_of_customers

puts cust2.display_details
puts cust2.total_no_of_customers
puts cust2.total_no_of_customers

class Sample

  def hello
    puts "成员函数"
    puts "Hello ruby!"
  end
end

obj=Sample.new
obj.hello


http://www.runoob.com/ruby/ruby-variable.html

http://rubyinstaller.org/
实例源码下载(右键保存图片,重命名为rar即可):

ruby教程 ruby开发教程_ruby教程