Ruby学习笔记-第一章

Ruby的运行方法
1.ruby命令
2.irb(小程序)
3.交互式命令

Ruby命令
print("hello,ruby. ") 保存为helloruby.rb
C:UsersAdministrator>ruby helloruby.rb

irb(小程序)
C:UsersAdministrator>irb
irb(main):001:0> print("hello ")
hello
=> nil--(print返回值)
exit、Ctrl+d终止irb

输出换行的两种方式
irb(main):003:0> print("hello ruby ")
hello ruby
=> nil
irb(main):009:0> print("hello)
irb(main):010:1" ruby")
hello)
ruby=> nil

引号
“ ”:双引号之间的内容特殊字符会转义
‘ ’:不转义
:转义字符

方法的调用
ruby调用方法时括号可以省略
irb(main):011:0> print "hello "
hello
=> nil
多个字符用,分开
irb(main):012:0> print "apple","orange"
appleorange=> nil

puts方法
puts和print的区别是,puts方法在输出结果的末尾一定会输出换行符
irb(main):013:0> puts "hello"
hello
=> nil
irb(main):014:0> print "hello"
hello=> nil
irb(main):015:0>
当参数是多个字符串的时候,各个末尾都加换行符。
irb(main):015:0> puts "hello ","Ruby"
hello
Ruby
=> nil
irb(main):016:0> print "hello ","Ruby"
hello Ruby=> nil

P方法-提供给编程者使用的
可以显示字符和数字的区别
irb(main):017:0> puts 100
100
=> nil
irb(main):018:0> puts "100"
100
=> nil
irb(main):019:0> p 100
100
=> 100
irb(main):020:0> p "100"
"100"
=> "100"
但是换行符和制表符不会转义
p "hello "
irb(main):021:0> puts "hello "
hello

=> nil

irb(main):022:0> p "hello "
"hello "
=> "hello "

编码方式
# encoding:GBK Windows
# encoding:UTF-8 Max
# encoding:UTF-8 Unix
控制台加”-E 编码方式“ 指定输出结果的编码方式
ruby -E UTF-8 脚本文件名 (脚本执行)
irb -E UTF-8              (irb启动)

数学相关的函数
include Math
或者
Math.sin
irb(main):024:0> Math.sqrt(100)
=> 10.0

注释
#单行注释
=begin
多行注释
=end

条件判断
if 条件 then
    语句
else
    语句
end

循环
while 循环条件 do
    循环处理
end

times方法
循环次数.times do
    循环处理
end
irb(main):025:0> 3.times do
irb(main):026:1* print "hello "
irb(main):027:1> end
hello
hello
hello
=> 3

原文地址:https://www.cnblogs.com/3ddan/p/10431970.html