【Ruby】语法记录一
来源:互联网 发布:淘宝做代理赚钱吗 编辑:程序博客网 时间:2024/06/05 06:02
#
- 接受用户输入
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')
- unless
unless 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-10for 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
0 0
- 【Ruby】语法记录一
- Ruby 语法(一)
- Ruby语言基础学习一:Ruby语法和数据类型
- Lua 语法学习记录一
- ruby语法
- ruby-语法
- ruby语法
- [ruby] 语法
- Ruby语法
- ruby语法
- Ruby 语法
- ruby 语法
- Ruby学习记录 - 安装Ruby
- ruby 语法的总结
- Ruby语法学习笔记
- ruby的基本语法
- 基本的ruby语法
- Ruby 语法(二)
- 探索Popupwindow-对话框风格的窗口(
- 【Ruby】语言基础
- 测试博客
- 【Java】Java、JavaScript、JSP、JScript,傻傻分不清楚
- HDU 5146 Sequence
- 【Ruby】语法记录一
- mysql下载安装配置
- 10009---AngularJS Http
- 阿里的分布式数据库DRDS
- Map中entry的源代码
- Android学习笔记(3):Android项目结构分析
- 【ZYNQ-7000开发之三】ZYNQ平台的HDMI驱动测试
- 第14章1节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-面向控件编程VS面向坐标编程
- 第14章2节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-HierarchyViewer架构概述