2:比较运算符:
age = 10
puts "You are age too young" if age < 18
循环:5.times do puts "Test" end <=> 5.times {puts "Test"}
3:浮点数
x = 3
y = 3.6
x.to_f
y.to_i
4:常量
大写字母开头的变量
5:文本和字符串
5.1:字符串是一组任意长度的文本字符,ruby中所用的字符串都是String类的对象 字符串有单引号或者双引号括起来的文本字符
5.2 利用%q{}可以扩展到多行 {} 可以用其他的分界符代替 <> []
5.3 引入文档 x=<<End_MY_STRING
This is the string
6:字符串表达式
6.1 + > < * ?x 获取x字符的对应的ASCII值 120.chr为相反效果
6.2 插写 #{x} #{"bad"}
6.3 正则表达式就是搜索查询
puts "foobar".sub("bar","foo")#将"bar" 替换为 "foo" 只替换一次 gsub替换全部
x = "This is test"
puts x.sub(/^../,"Hel") #将开头的字符串加上Hel
"xyz".scan(/./){|letter| puts letter}#将字符串迭代输出
正则表达式:
7:数组
=> ["find", "[]=", "inspect", "tap", "flatten", "<<", "reject!", "&", "clone", "rindex", "take", "push", "object_id", "__send__", "public_methods", "reject", "transpose", "instance_variable_defined?", "minmax", "choice", "freeze", "equal?", "member?", "each", "indexes", "delete", "*", "sort", "extend", "partition", "map!", "uniq", "+", "each_cons", "send", "any?", "each_with_index", "to_ary", "methods", "pack", "values_at", "-", "detect", "hash", "dup", "take_while", "join", "pop", "replace", "instance_variables", "to_enum", "reverse", "clear", "collect", "slice!", "permutation", "min_by", "indices", "|", "sort_by", "eql?", "size", "enum_cons", "group_by", "id", "instance_eval", "flatten!", "one?", "enum_with_index", "at", "singleton_methods", "delete_at", "find_index", "nitems", "taint", "empty?", "drop", "shift", "instance_variable_get", "enum_for", "map", "frozen?", "fill", "display", "instance_of?", "combination", "max_by", "uniq!", "method", "grep", "to_a", "first", "compact", "instance_exec", "type", "none?", "reverse_each", "fetch", "protected_methods", "find_all", "<=>", "delete_if", "==", "min", "reverse!", "insert", "===", "drop_while", "unshift", "instance_variable_set", "concat", "sort!", "assoc", "each_slice", "inject", "respond_to?", "kind_of?", "minmax_by", "product", "count", "to_s", "class", "last", "shuffle!", "index", "zip", "private_methods", "=~", "tainted?", "__id__", "select", "shuffle", "length", "max", "untaint", "nil?", "each_index", "entries", "cycle", "collect!", "rassoc", "slice", "enum_slice", "reduce", "include?", "is_a?", "compact!", "[]", "all?"]
irb(main):010:0>
7.1 x=[1,2,3,4] x[1]#索引序号 从0开始 x<<"H" 向数据末尾加入内容
x.pop x.length
x.join #将数组中所有元素连接起来形成一个大的字符串 x.join(", ")分隔符
"This is a test".scan(/\w/).join(',')#字符串切割 inspect 他能给出对象的文本化表示形式
7.2数组迭代
x.each{|element| puts element.to_s +"x"}
x.collect{|element| element * 2}#对数组进行实时转换
7.3 其他方法
+ 、-
x.empty?#是否为空
x.include?("3") #是否包含某个元素
x.first
x.last#x.first(x) x.last(2) 指定参数得到数组从开始或者最末起的n个元素
x.reverse
8:散列表:其中的对象不在列表中给定位置,而是给定一个指向对象的键(key) 有点像字典
x = {2,5,6,7}#转化后为:x={2=>5,6=>7} 如若这样 x = {2,5,6,7,6}就会报错
x.size
x[2]
8.1:迭代
x.each{|k,v| puts "#{k} #{v}"}
x.keys
x.delete_if{|k,v| v < 23}
9:流程控制:
if condition
code
elsif conditon
code
else
code
end
unless condition
code
end
?:三元运算符
case attr
when "attr"
code
else
end
while condition
code
end
until condition
code
end
10 日期与时间
=> ["inspect", "tap", "month", "usec", "clone", "public_methods", "__send__", "getlocal", "object_id", "ctime", "instance_variable_defined?", "equal?", "freeze", "hour", "+", "extend", "send", "methods", "-", "year", "hash", "dst?", "strftime", "dup", "getgm", "to_enum", "instance_variables", "zone", "utc?", "eql?", "mday", "instance_eval", "id", "to_i", "singleton_methods", "wday", "taint", "enum_for", "frozen?", "instance_variable_get", "getutc", "gmtoff", "instance_of?", "display", "gmt?", "to_a", "method", "day", "instance_exec", "type", "to_f", "<", "tv_sec", "protected_methods", "<=>", "localtime", "between?", "==", "min", "yday", ">", "===", "_dump", "instance_variable_set", "asctime", "respond_to?", "kind_of?", "gmt_offset", "succ", ">=", "utc", "to_s", "<=", "mon", "class", "tv_usec", "private_methods", "=~", "tainted?", "__id__", "gmtime", "isdst", "untaint", "nil?", "sec", "is_a?", "utc_offset"]
irb(main):020:0>
Time.now
大数字
Fixum Bignum
范围:
('a'..'z').to_a.each{|let| puts let}
a= [1,5,6,7,8,9]
puts a[1..5]
a[1..4] = [5,6,7,8,9]#设置元素
11:符号
符号是Ruby中独特的概念,符号不包含值或者对象 ,不像变量那样而是用作代码中固定的名字 ,符号只是引用值 不占用内存
curent_situation = :good
pust "Everthing is fine" if curent_situation == :good
散列表中用来区分键和值
s = {:key=>'value'}
可以用于规约或统一使用那个键名
p = {:name=>"red"}
12:类间转化
Bignum Float Integer String Symbol
:to_i to_ to_f to_sym