脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Ruby - Ruby语法笔记

Ruby语法笔记

2020-05-07 11:02脚本之家 Ruby

本文给大家记录的是本人学习ruby之后所记录下来的部分语法知识,分享给有需要的小伙伴,希望对大家能够有所帮助。

接受用户输入

?
1
first_name = gets.chomp

首字母大写

?
1
first_name.capitalize!

字母变大写

?
1
first_name.upcase!

字母变小写

?
1
first_name.downcase!

多行输出

?
1
2
3
print <<EOF
  # 多行输出
EOF

注释

?
1
# 我是注释

变量获取

?
1
#{first_name}

变量

  1. 全局变量 $
  2. 类变量 @@
  3. 方法变量 @
  4. 局部变量 小写字母或_

if/else

?
1
2
3
4
5
if a < b
  puts '1'
elsif b < a
  puts '2'
end   

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Classname
  def functionname(params)
    puts params
  end
end
 
class1 = Classname.new
class1.functionname('1')
unless
unless false
  print 'ok'
else
  print 'no'
end   

是否包含字符

?
1
2
3
4
5
6
7
8
9
print 'puts'
 
user_input = gets.chomp
 
user_input.downcase!
 
if user_input.include?"s"
  print 'has s'
end

替换字符

?
1
2
# s -> th
user_input.gsub!(/s/,"th")

在字符串中输出变量值

?
1
puts 'okok #{user_input}'

while

?
1
2
3
4
5
6
counter = 1
 
while counter < 11
  puts counter
  counter = counter + 1
end

Until

?
1
2
3
4
5
6
counter = 1
until counter > 10
  print counter
  counter = counter + 1
end

+= 、 -= 、 *=、 /=
Some languages have the increment operators ++ and -- (which also add or subtract 1 from a value), but Ruby does not
for循环
# 如果 1...10 包含1-9,如果 1..10 包含1-10

?
1
2
3
for num in 1...10
  puts num
end

Loop Method
An iterator is just a Ruby method that repeatedly invokes a block of code.

?
1
2
3
4
5
6
i = 20
loop do
  i -= 1
  print "#{ i }"
  break if i <= 0
end

Next

?
1
2
3
4
5
6
7
i = 20
loop do
 i -= 1
 next if i%2 != 0
 print "#{i}"
 break if i <= 0
end

数组

?
1
my_array = [1,2,3,4,5]

The .each Iterator迭代器

?
1
2
3
4
5
6
7
8
9
numbers = [1, 2, 3, 4, 5]
 
# one way to loop
numbers.each { |item| puts item }
 
# another way to loop
numbers.each do |item|
 puts item
end

The .times Iterator 次数迭代器

?
1
10.times { print 'ok'})

Looping with 'While'

?
1
2
3
4
5
6
num = 1
 
while num <= 50 do
  print num
  num += 1
end

Looping with 'Until'

?
1
2
3
4
5
num = 1
until num > 50 do
  print num
  num += 1
end

Loop the Loop with Loop

?
1
2
3
4
5
6
7
num = 0
 
loop do
  num += 1
  print "Ruby!"
  break if num == 30
end

The .split Method,

?
1
2
3
4
5
6
7
8
9
10
11
12
text.split(",")
 
puts "Text to search through: "
text = gets.chomp
puts "Word to redact"
redact = gets.chomp
 
words = text.split(" ")
 
words.each do |word|
  print word
end

延伸 · 阅读

精彩推荐
  • RubyRuby迭代器的7种技巧分享

    Ruby迭代器的7种技巧分享

    这篇文章主要介绍了Ruby迭代器的7种技巧分享,Ruby中的迭代器非常人性化,本文既是讲解了7个技巧也是讲解了7种迭代器,需要的朋友可以参考下 ...

    脚本之家4782020-04-20
  • Ruby简要说明Ruby中的迭代器

    简要说明Ruby中的迭代器

    这篇文章主要介绍了Ruby中的迭代器,迭代器的概念在动态语言的编程中十分重要,文章中介绍了Ruby中的each迭代器和collect迭代器,需要的朋友可以参考下 ...

    goldensun2772020-04-25
  • RubyRuby设计模式编程中使用Builder建造者模式的实例

    Ruby设计模式编程中使用Builder建造者模式的实例

    这篇文章主要介绍了Ruby设计模式编程中使用Builder建造者模式的实例,建造者模式将一个复杂对象的构造与它的表示分离,使同样的构建过程可以创建不同的表...

    范孝鹏2192020-05-07
  • RubyRuby进行文件信息输出实例代码

    Ruby进行文件信息输出实例代码

    Ruby进行文件信息输出实例代码,数据是随机的,所以每次的记录都会不同。 ...

    ruby教程网2962020-04-10
  • Ruby剖析 Ruby 访问控制

    剖析 Ruby 访问控制

    前面,我们说 Ruby 没有函数,只有方法.而且实际上有不止一种方法.这一节我们介绍 访问控制 (accesscontrols). 想想当我们在最高层而不是在一个类的定义里定义...

    ruby教程网3572020-04-08
  • RubyCentOS中配置Ruby on Rails环境

    CentOS中配置Ruby on Rails环境

    经过一个上午的折腾,终于把ROR环境在CentOS中搞定,绕了很多弯路,把文章写下来总结一下 ...

    可乐加糖4762020-04-12
  • RubyRuby简洁学习笔记(一):字符串、数字、类和对象

    Ruby简洁学习笔记(一):字符串、数字、类和对象

    这篇文章主要介绍了Ruby简洁学习笔记(一):字符串、数字、类和对象,本文是学习笔记第一篇,需要的朋友可以参考下 ...

    脚本之家2472020-04-20
  • RubyRuby环境下安装使用bundler来管理多版本的gem

    Ruby环境下安装使用bundler来管理多版本的gem

    这篇文章主要介绍了Ruby环境下安装使用bundler来管理多版本的gem的方法,举了Ruby On Rails中的应用实例来进行演示,需要的朋友可以参考下 ...

    日拱一卒4332020-05-10