Ruby语法笔记
技术  /  管理员 发布于 6年前   291
接受用户输入
first_name = gets.chomp
首字母大写
first_name.capitalize!
字母变大写
first_name.upcase!
字母变小写
first_name.downcase!
多行输出
print <<EOF # 多行输出EOF
注释
# 我是注释
变量获取
#{first_name}
变量
if/else
if a < b puts '1'elsif b < a puts '2'end
类
class Classname def functionname(params) puts params endendclass1 = Classname.newclass1.functionname('1')unlessunless false print 'ok'else print 'no'end
是否包含字符
print 'puts'user_input = gets.chompuser_input.downcase!if user_input.include?"s" print 'has s'end
替换字符
# s -> thuser_input.gsub!(/s/,"th")
在字符串中输出变量值
puts 'okok #{user_input}'
while
counter = 1while counter < 11 puts counter counter = counter + 1end
Until
counter = 1until counter > 10 print counter counter = counter + 1end
+= 、 -= 、 *=、 /=
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
for num in 1...10 puts numend
Loop Method
An iterator is just a Ruby method that repeatedly invokes a block of code.
i = 20loop do i -= 1 print "#{ i }" break if i <= 0end
Next
i = 20loop do i -= 1 next if i%2 != 0 print "#{i}" break if i <= 0end
数组
my_array = [1,2,3,4,5]
The .each Iterator迭代器
numbers = [1, 2, 3, 4, 5]# one way to loopnumbers.each { |item| puts item }# another way to loopnumbers.each do |item| puts itemend
The .times Iterator 次数迭代器
10.times { print 'ok'})
Looping with 'While'
num = 1while num <= 50 do print num num += 1end
Looping with 'Until'
num = 1until num > 50 do print num num += 1end
Loop the Loop with Loop
num = 0loop do num += 1 print "Ruby!" break if num == 30end
The .split Method,
text.split(",")puts "Text to search through: "text = gets.chompputs "Word to redact"redact = gets.chompwords = text.split(" ")words.each do |word| print wordend
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号