Install
sudo apt install ruby ruby-dev ruby-bundler
root@ubuntu22:~# ruby --version
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux-gnu]
root@ubuntu22:~# bundler --version
Bundler version 2.3.5
===== 默认源
root@ubuntu22:~/ruby# gem sources
*** CURRENT SOURCES ***
https://rubygems.org/
===== 国内源
gem sources --remove https://rubygems.org/
gem sources -a https://mirrors.aliyun.com/rubygems/
Ruby 语法
print "Hello world!\n"
# 输出 p(”hee")
print("hello")
print("\n")
puts "hello"
# 数值对象,字符串对象,时间对象,文件对象,数组对象
# Numeric, String, Array, File
# 局部变量 aa, _aa 常量(readonly) TEST=1 RUBY_VERSION
# 全局变量 $x 实例变量@xx 类变量@@shape
arr = ["a", 2, nil]
arr.each do |item|
p(item)
end
puts(RUBY_VERSION)
puts(RUBY_PLATFORM)
a=1
b=2;c=3
v=(a>b) ? a : b
puts(v)
# Range.new(1,10) 范围运算符1..10
# 指数** 三元?:
# 值相等 === , 属于类 p(String === "asdf")
选择循环
# if xx then [] end 可以省略then, if xx then {} elsif then {} else {} end
# unless yy then {} end 条件为假执行
if true
puts("bb")
end
case v
when 1 then
puts("1")
when 2 then
puts("2")
else
puts("none")
end
# 循环 times方法, for语句, while, util, loop
# 用{}代替 do end
3.times do |i|
puts("No. #{i}")
end
10.times { }
for i in 1..10 do
#
end
arr = ["a", "b", "c", "d"]
for name in arr do
#
end
arr.each do |name| puts name end
loop do
# 死循环
break # next等价continue, redo重复刚才处理
end
while true do
# 条件为真
end
until false do
# 调价为假
end
函数
# 运算符也是方法,有些可以重载
# 实例方法
p "10,2,3".split(",")
p [12,3].index(1)
p 100.to_s # 转字符串
# 类方法
Array.new
File.open("hello.rb", "r")
Time.now
# 函数方法
sleep(2)
print("hello world")
def hello(name)
puts "Hello, #{name}"
end
def add(a,b)
return a + b # 可以省略return
end
def fool(*args)
# 个数不确定
# 至少一个参数 def add(a, *args)
end
面向对象
arr=Array.new
p arr
p arr.class
arr = [1,2]
str = "hello world"
p arr.class
p str.class
p str.instance_of?(String)
# 实例变量@xx, 类变量@@yy所有实例共享
class HelloWorld
@@count = 0
def initialize(myname="Ruby")
@name=myname
@@count+=1
end
def hello
puts "Hello #{@name} / #{@@count}"
end
end
hx = HelloWorld.new("Tomas")
hx2 = HelloWorld.new
hx.hello
# 类方法
# class HelloStatic
# class << self
# def hello(name)
# puts "Hello static #{name}"
# end
# end
# end
# 类方法2, 类常量
class HelloStatic
VERSION="v1.0"
end
def HelloStatic.hello(name)
puts "Hello static #{name}"
end
HelloStatic.hello("Ruby")
puts HelloStatic::VERSION
# 访问级别 public(默认), private, protected
class AccTest
def hello
end
public :hello
def name
end
private :name
end
acc = AccTest.new
acc.hello
# 继承
class ArrayTest < Array
def[](i)
super(0)
end
end
arr = ArrayTest.new([1,2,3])
puts arr[0]
puts arr[1]
puts arr[2]
# 模块, 命名空间
module Std
STD_VERSION = "v17" # 模块常量
def hello
end
module_function :hello # 模块方法
end
Std::hello
puts Std::STD_VERSION
include Std
puts STD_VERSION
异常
begin
# File.open("aaa.txt") # IOError
# 抛出异常, RuntimeError
raise
raise("xyzwer")
rescue => ex
# 错误处理, StandardError及其子类(RuntimeError)
puts ex.message
ensure
puts "finally" # 无论是否发生异常
end