学习背景:在使用logstash过程中,需要使用filter下的ruby插件
一、安装ruby
1. 下载ruby,下载链接:https://rubyinstaller.org/downloads/
2. 执行安装文件,安装ruby,安装目录修改为E盘,其他默认
3. 查看结果
ruby -v
二、交互式 Ruby(IRb)
1. 打开cmd命令窗口,依次执行如下命令
irb
def hello
out = "hello world"
puts out
end
nil
hello
2. 查看命令显示的结果
三、入门学习
1. 第一个ruby脚本
1.1 first_ruby.rb脚本文件内容
#!/usr/bin/env ruby
# frozen_string_literal: true
puts 'Hello World!'
1.2 切换到脚本文件所在目录,执行脚本(此处用Cygwin终端演示)
cd F:/IdeaProjects/my-jooq/my-ruby
ruby first_ruby.rb
2. Here Document
2.1 multi_line_string.rb脚本文件内容
#!/usr/bin/ruby
# frozen_string_literal: true
print <<EF
这是第一种方式创建here document 。
多行字符串。
EF
print <<"EF"
这是第二种方式创建here document 。
多行字符串。
EF
# 执行命令
print <<'EC'
echo hi there
echo lo there
EC
# 进行堆叠
print <<"FOO", <<"BAR"
I said foo.
FOO
I said bar.
BAR
2.2 执行结果(此处使用IDEA执行)
E:\Ruby27-x64\bin\ruby.exe F:/IdeaProjects/my-jooq/my-ruby/multi_line_string.rb
这是第一种方式创建here document 。
多行字符串。
这是第二种方式创建here document 。
多行字符串。
echo hi there
echo lo there
I said foo.
I said bar.
Process finished with exit code 0
3. begin、end语法
3.1 begin_end.rb脚本内容
#!/usr/bin/ruby
# frozen_string_literal: true
puts '这是主 Ruby 程序'
at_exit do
puts '停止 Ruby 程序'
end
# 避免使用
BEGIN {
puts '初始化 Ruby 程序'
}
3.2 执行结果
E:\Ruby27-x64\bin\ruby.exe F:/IdeaProjects/my-jooq/my-ruby/begin_end.rb
初始化 Ruby 程序
这是主 Ruby 程序
停止 Ruby 程序
Process finished with exit code 0
4. 数据类型
4.1 data_typy.rb脚本内容
#!/usr/bin/ruby
# frozen_string_literal: true
# 整形
a1 = 123
# puts不带回车换行符
print(a1)
a2 = 123_455
print(' ')
print(a2)
a3 = 1_123_455
print(' ')
print(a3)
print(' ')
# 浮点型
f3 = 1_000_000.1
puts f3
message = format('Processing of the data has finished in %<time>d seconds',
time: 5)
# puts带回车换行符
puts message
# 多个参数
puts format('a1: %<first>d, a2: %<second>d', first: a1, second: a2)
puts format('%<first>d %<second>d', first: 20, second: 10)
puts "相乘 : #{24 * 60 * 60}"
name = 'Ruby'
puts (name + ', ok').to_s
# 数组
arr = ['abc', 123, 3.1415, 'efg', 'last']
arr.each do |i|
puts i
end
# hash
colors = { red: 0xf00, green: 0x0f0, blue: 0x00f }
colors.each do |key, value|
print key, ' is ', value, "\n"
end
# 范围
(10..15).each { |n| print n, ' ' }
4.2 执行结果
E:\Ruby27-x64\bin\ruby.exe F:/IdeaProjects/my-jooq/my-ruby/data_typy.rb
123 123455 1123455 1000000.1
Processing of the data has finished in 5 seconds
a1: 123, a2: 123455
20 10
相乘 : 86400
Ruby, ok
abc
123
3.1415
efg
last
red is 3840
green is 240
blue is 15
10 11 12 13 14 15
Process finished with exit code 0
5. 类和对象
5.1 class_and_object.rb脚本内容
#!/usr/bin/ruby
# frozen_string_literal: true
# 类上必须加注释,否则提示警告:
# RuboCop: Missing top-level class documentation comment. [Style/Documentation]
class Person
attr_reader :name, :age
# 定义实例方法
def hello
puts 'Hello Ruby'
end
end
# 不带参数创建对象
person = Person.new
puts person
# 调用实例方法
person.hello
# 消费者类继承人类
class Customer < Person
# @@ 开头的是类变量,初始化类变量
@@count = 0
# class_variable_set(:@@count, 0)
# 调用带参数的类的 new 方法时执行
def initialize(id, name, addr)
# @ 开头的是实例变量
@cust_id = id
@cust_name = name
@cust_addr = addr
end
def self.count
@@count
end
def display_details
puts "Customer id #{@cust_id}"
puts "Customer name #{@cust_name}"
puts "Customer address #{@cust_addr}"
end
def self.total_count
@@count += 1
puts "Total number of customers: #{@@count}"
end
end
# 带参数创建对象
cust1 = Customer.new('1', 'zhangsan', '北京')
cust2 = Customer.new('2', 'lisi', '深圳')
puts cust1.display_details, Customer.total_count,
cust2.display_details, Customer.total_count
5.2 执行结果
E:\Ruby27-x64\bin\ruby.exe F:/IdeaProjects/my-jooq/my-ruby/class_and_object.rb
#<Person:0x0000000006407670>
Hello Ruby
Customer id 1
Customer name zhangsan
Customer address 北京
Total number of customers: 1
Customer id 2
Customer name lisi
Customer address 深圳
Total number of customers: 2
Process finished with exit code 0
6. 循环
6.1 cycle.rb脚本文件内容
#!/usr/bin/ruby
# frozen_string_literal: true
# while 语句
m1 = 0
while m1 < 5
puts("Inside the loop m1 = #{m1}")
m1 += 1
end
puts
# while 修饰符
m2 = 0
begin
puts("Inside the loop m2 = #{m2}")
m2 += 1
end while m2 < 5
puts
# until 修饰符
m3 = 0
begin
puts("Inside the loop m3 = #{m3}")
m3 += 1
end until m3 >= 5
puts
m4 = 0
# until 语句
until m4 >= 5
puts("Inside the loop m4 = #{m4}")
m4 += 1
end
# for 语句
puts
(0..5).each do |m5|
puts "The value of the local variable m5 is #{m5}"
end
# each 语句
puts
(0..5).each do |m6|
puts "The value of the local variable m6 is #{m6}"
end
# break 语句
puts
(0..5).each do |m7|
break if m7 > 2
puts "The value of the local variable m7 is #{m7}"
end
# next 语句
puts
(0..5).each do |m8|
next if m8 < 3
puts "The value of the local variable m8 is #{m8}"
end
# redo 语句
puts
i = 0
(0..5).each do |m9|
break if i == 5
puts "The value of the local variable m9 is #{m9}"
if m9 == 3
i += 1
redo
end
end
# retry 语句
puts
n = 0
begin
(0..5).each do |m10|
break if n == 3
raise 'A test exception.' if m10 == 3
puts "The value of the local variable m10 is #{m10}"
end
rescue StandardError
n += 1
retry
end
6.2 执行结果
E:\Ruby27-x64\bin\ruby.exe F:/IdeaProjects/my-jooq/my-ruby/cycle.rb
Inside the loop m1 = 0
Inside the loop m1 = 1
Inside the loop m1 = 2
Inside the loop m1 = 3
Inside the loop m1 = 4
Inside the loop m2 = 0
Inside the loop m2 = 1
Inside the loop m2 = 2
Inside the loop m2 = 3
Inside the loop m2 = 4
Inside the loop m3 = 0
Inside the loop m3 = 1
Inside the loop m3 = 2
Inside the loop m3 = 3
Inside the loop m3 = 4
Inside the loop m4 = 0
Inside the loop m4 = 1
Inside the loop m4 = 2
Inside the loop m4 = 3
Inside the loop m4 = 4
The value of the local variable m5 is 0
The value of the local variable m5 is 1
The value of the local variable m5 is 2
The value of the local variable m5 is 3
The value of the local variable m5 is 4
The value of the local variable m5 is 5
The value of the local variable m6 is 0
The value of the local variable m6 is 1
The value of the local variable m6 is 2
The value of the local variable m6 is 3
The value of the local variable m6 is 4
The value of the local variable m6 is 5
The value of the local variable m7 is 0
The value of the local variable m7 is 1
The value of the local variable m7 is 2
The value of the local variable m8 is 3
The value of the local variable m8 is 4
The value of the local variable m8 is 5
The value of the local variable m9 is 0
The value of the local variable m9 is 1
The value of the local variable m9 is 2
The value of the local variable m9 is 3
The value of the local variable m9 is 3
The value of the local variable m9 is 3
The value of the local variable m9 is 3
The value of the local variable m9 is 3
The value of the local variable m10 is 0
The value of the local variable m10 is 1
The value of the local variable m10 is 2
The value of the local variable m10 is 0
The value of the local variable m10 is 1
The value of the local variable m10 is 2
The value of the local variable m10 is 0
The value of the local variable m10 is 1
The value of the local variable m10 is 2
Process finished with exit code 0
7. 条件判断
7.1 conditional_judgment.rb脚本内容
#!/usr/bin/ruby
# frozen_string_literal: true
# if...else 语句
x1 = 1
if x1 > 2
puts 'x1 is greater than 2'
elsif (x1 <= 2) && (x1 != 0)
puts 'x1 is 1'
else
puts "I can't guess the number"
end
# if 修饰符
x2 = 1
print "x2=#{x2}\n" if x2
# unless 语句
x3 = 1
unless x3 > 2
puts 'x3 is less than 2'
else
puts 'x3 is greater than 2'
end
# unless 修饰符 如果 conditional 为假,则执行 code
x4 = 1
print "1 -- Value is set\n" if x4
print "2 -- Value is set\n" unless x4
x5 = false
print "3 -- Value is set\n" unless x5
# case 语句
age = 5
case age
when 0..2
puts 'baby'
when 3..6
puts 'little child'
when 7..12
puts 'child'
when 13..18
puts 'youth'
else
puts 'adult'
end
7.2 执行结果
E:\Ruby27-x64\bin\ruby.exe F:/IdeaProjects/my-jooq/my-ruby/conditional_judgment.rb
x1 is 1
x2=1
x3 is less than 2
1 -- Value is set
3 -- Value is set
little child
Process finished with exit code 0