在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1、方法 定义如下所示: def string_message(str='') if str.empty? "It's an empty string!" else "The string is nonempty." end end 参数:' '是参数str的默认值,调用函数时,str参数是可选的,如果不指定,就使用默认值。参数名称任意。 下面方法和上面的方法等价: def string_message(str='') return "It's an empty string!" if str.empty? return "The string is nonempty." end 2、unless的用法 #!/usr/bin/ruby x=1 unless x>2 puts "x is less than 2" else puts "x is greater than 2" end 用!!对对象做两次取反操作,把对象转换成布尔值。Ruby中nil是唯一一个布尔值为“假”的对象,其他所有Ruby对象都是“真”值,数字0也是。 >> nil => false >> !0 => true >> "foobar".length >> "foobar".empty? >> "foobar".include?("foo") >> puts "Both strings are empty" if x.empty? && y.empty? >> nil.to_s => "" >> nil.to_s.empty? => true 3、数组(array) (1)split方法的使用 >> "foo bar baz".split #把字符串拆分成幼三个元素的数组,默认在空格处进行拆分 => ["foo","bar","baz"] >> "fooxbarxbazx".split('x') => ["foo","bar","baz"] (2)索引 索引可以是负数 >> a=[3,4,2] >> a[0] => 3 >> a[-1] => 2 a.first a.second a.last a.length a.empty? a.include?(42) 4、值域(range) >> (0..5).to_a => [0,1,2,3,4] >> a=%w[foo bar baz quux] => ["foo","bar","baz","quux"] >> a[0..2] => ["foo","bar","baz"] >> a=(0..9).to_a => [0,1,2,3,4,5,6,7,8,9] >> a[2..(a.length-1)] => [2,3,4,5,6,7,8,9] >> a[2,-1] => [2,3,4,5,6,7,8,9] >> ('a'..'e').to_a => ["a","b","c","d","e"] 5、块 约定:当块中只有一行简单的代码时使用花括号形式;当块是一行很长的代码,或者有很多行时使用do..end形式。例如: >> (1..5).each{|i| puts 2*i} (1..5).each do |i| puts 2*i puts '-' end >> 3.times{puts "betelgeuse"} >> (1..5).map{|i| i**2} >> %w[a b c].map{|char| char.upcase} >> %w[A B C].map{|char| char.downcase} >> %w[A B C].map(&:downcase) >> ('a'..'z').to_a.shuffle[0..7].join #生成随机二级域名 6、哈希和符号 哈希(hash)本质上就是数组,它的索引不局限于数字。哈希的索引可以使用任何对象。哈希通过一对花括号包含一些键值对的形式表示。哈希和数组最大的区别在于:哈希中的元素没有特定的顺序。哈希中没有定义的键对应的值是nil >> user={} #{}是一个空哈希 >> user["first_name"]="Amy" >> user["last_name"]="Sun" >> user["first_name"] => "Amy" >> user => {"first_name"=>"Amy","last_name"=>"Sun"} >> user["e-mail"] => nil >> user={ "first_name"=>"Amy","last_name"=>"Sun" } =>{"first_name"=>"Amy","last_name"=>"Sun"} 符号:Ruby中特有的数据类型,是没有约束的字符串 >> user={ :name=>"AmySun",:email=>"12***22@**.com" } => {:name=>"AmySun",:email=>"12***22@**.com"} >> user[:name] => "AmySun" >> user[:password] => nil >> h1={ :name=>"AmySun",:email=>"12***22@**.com" } => {:name=>"AmySun",:email=>"12***22@**.com"} >> h1={ name:"AmySun",email:"12***22@**.com" } #:name=>和name:的作用一样,因此{ :name=>"AmySun" }和{ name:"AmySun" }是等效的,但是只有:name表示符号 => {:name=>"AmySun",:email=>"12***22@**.com"} >> h1==h2 => true 嵌套哈希 >> params={} => {} >> params[:user]={ name:"AmySun",email:"12***22@**.com" } => {:name=>"AmySun",:email=>"12***22@**.com"} >> params => {:user=>{:name=>"AmySun",:email=>"12***22@**.com"}} >> params[:user][:email] => 12***22@**.com 哈希中的each方法 >> user={ success:"It worked!",danger:"It failed." } => {:success=>"It worked!",:danger=>"It failed."} user.each do |key,value| puts "Key #{key.inspect} has value #{value.inspect}" end # 结果 # Key :success has value "It worked!" # Key :failed has value "It failed." 其中inspect方法返回被调用对象的字符串字面量表现形式,例如: >> puts (1..5).to_a.inspect [1,2,3,4,5] >> puts :name,:name.inspect name :name >> puts "It worked!","It worked!".inspect It worked! "It worked!" >> p :name #等价于 ‘puts :name.inspect’ :name 7、引入css说明 <%= stylesheet_link_tag 'application',media:'all','data-trubolinks-track'=>true %> 说明:调用函数时,如果哈希是最后一个参数,可以省略花括号;因为data-trubolinks-track中有连字符,所以不能写成data-trubolinks-track:true的样式生成的html源码如下: <link data-trubolinks-track="true" href="/assets/application.css" media="alll" rel="stylesheet" />
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论