表明
变量set 和get
attr_accessor :required alias_method :required?, :required # @return [Boolean] # Indicates if the argument is repeatable (= can appear multiple # times in the command, which is indicated by '...' in the banner) # attr_accessor :repeatableattr_accessor :abc等同于def abcreturn @abcenddef abc=(abc)@abc = abcendruby中的alias和alias_method
new_name 和old_name
(12)alias :new_name ld_name 可以用来给方法取别名。alias是个关键字,和他类似的尚有一个Module#alias_method方法
https://blog.csdn.net/raosheng1993/article/details/45458821 的第12点
if 语法
https://haicoder.net/ruby/ruby-else.html 这内里有全部的if语法记载
message = verbose_prefix + message if config.verbose? puts_indented message if config.verbose?假如 if 语句条件为真,则会实行相对应的代码
#!/usr/bin/ruby -w# -*- coding : utf-8 -*-puts "HaiCoder(www.haicoder.net)"$debug=1print "debug\n" if $debug步伐运行后,控制台输出如下:
由于 debug 变量为 1,以是我们这里输出了 debug。
Ruby if修饰符总结
在 Ruby 中,if 修饰词组表现当 if 右边之条件成立时才实行 if 左边的式子。即假如 conditional 为真,则实行 code。
:语法
def repo_update?(default: false) if @repo_update.nil? default else @repo_update end end def initialize(argv) @repo_update = argv.flag?('repo-update') super end在Ruby中有很多方法是以?和!号末了的
“?”被用于标示谓词,即返回Boolean直的方法,如Array.empty?(判断数组中元素是否为空)
“!”出现在方法名尾部的感叹号表明使用该方法是必要多加警惕。很多Ruby的焦点类都界说了
成对的方法,它们具有同样的名称,只是末了相差一个“!”,通常环境下,不带感叹号的方法返
调用该方法的一个拷贝,二带感叹号的方法则是一个可变方法,该方法会修改原来的对象,如Array
类中的sort和sort!
ruby中的方法可以以问号和叹号末了,问号通常用于谓语方法,这种方法返回一个布尔值。比方array和hash类都界说了一个empty?方法,这个方法用于测试数据结构中有没有元素。
假如方法以叹号末了,这意味着我们在使用这个方法的时间要警惕,比如大多数焦点的ruby类库方法都提供两个同名的方法,一个以叹号末了,一个没有,
区别在于,假如使用没有叹号末了的方法,你在调用它的时间会得到当前对象的一个拷贝而不会修改原始对象,而假如使用带有叹号的方法,你在调用它的时间会直接修改当前对象的值。
叹号 !
2.以感叹号末了的方法。一样平常表现这是伤害的,大概会修改吸收者对象的方法。
def insert_sort! (0...self.length).to_a.each do |j| key = self[j] i = j - 1; while i >= 0 and self > key self[i+1] = self i = i-1 end self[i+1] = key end self end`运行上述代码之后,会对传入的数据举行排序,修改了(吸收者)对象。
Ruby焦点类都界说了成对的方法,它们有同样的名字,相互的差异在于此中一个以感叹号末了,而另一个没有,通常环境下,不带感叹号的方法返回调用该方法的对象的一个修改过的拷贝,而带感叹号的方法则是一个可变的方法,该方法会修改原对象。
3.以等号末了的方法。一样平常被赋值的方法以等号末了。
def validate!
def validate!
<<
``` section << " (branch `#{@branch}`)" if @branch```方法传参 (关键字参数)
extend:在界说类时使用,把module的实例方法作为当前类的类方法.
module Test def class_type "This class is of type:#{self.class}" endendclass TestClass extend Testendputs TestClass.class_type #=> This class is of type:Classmodule Pod class Command class Repo < Command self.abstract_command = true # @todo should not show a usage banner! # self.summary = 'Manage spec-repositories' self.default_subcommand = 'list' #-----------------------------------------------------------------------# extend Executable executable :git def dir config.repos_dir + @name end end endend=> 箭头函数
=>将键与哈希映射笔墨中的值分开。它不可重载,也没有专门毗连到符号。
哈希图笔墨的情势为{key1 => value1, key2 => value2, ...},但是当用作函数的最后一个参数时,可以省略花括号。因此,当您看到类似f(:a => 1, :b => 2)的函数调用时,将使用一个参数调用f,这是一个具有键:a和:b以及值1和2的哈希映射。
def pre_download(sandbox) title = "re-downloading: `#{name}` #{description}" UI.titled_section(title, :verbose_prefix => '-> ') do target = sandbox.pod_dir(name) begin download_result = Downloader.download(download_request, target, :can_cache => can_cache) rescue Pod:SLError => e raise Informative, "Failed to load '#{name}' podspec: #{e.message}" rescue => e raise Informative, "Failed to download '#{name}': #{e.message}" end spec = download_result.spec raise Informative, "Unable to find a specification for '#{name}'." unless specdef download_request Downloader::Request.new( :name => name, :params => params, ) end? !
=begin Ruby program to demonstrate Hash.key? method=end hsh = {"colors" => "red","letters" => "a", "Fruit" => "Grapes"}puts "Hash.key? implementation:"puts "Enter the Key you want to search: "ky = gets.chompif (hsh.key?(ky)) puts "Key found successfully"else puts "Key not found!"end输出
Hash.key? implementation:Enter the Key you want to search: colorsKey found successfully在上面的代码中,您可以观察到我们在平凡的Hash实例上调用Hash.key?()方法。当它在用户输入的哈希对象中发现键存在时,它返回true。
范例2:
=begin Ruby program to demonstrate Hash.key? method=end hsh = {"colors" => "red","letters" => "a", "Fruit" => "Grapes"}hsh1 = {"cars" => "800","bike" => "pulsar", "phone" => "A50"}hsh2 = {"one"=> hsh, "two" => hsh1}puts "Hash.key? implementation:"puts "Enter the Key you want to search: "ky = gets.chompif (hsh2.key?(ky)) puts "Key found successfully"else puts "Key not found!"end输出结果
Hash.key? implementation:Enter the Key you want to search: colorsKey not found!阐明: