zoukankan      html  css  js  c++  java
  • [Ruby] LEVEL 2 Methods and Classes

    Optional Arguments

    Set default arguments, when we don't need to call it, we can simply skip it.

    def new_game(name, year=nil, system=nil)
      {
        name: name,
        year: year,
        system: system
      }
    end
    game = new_game("Street Figher II")

    Options Hash Argument

    Sometimes, optinal argumetns also not that good. For exmaple, we have one argument added to the end, if we do want pass in reply_id and year, system we don't need to pass in, then we need to put placeholder in the function call.

    def new_game(name, year=nil, system=nil, reply_id = nil)
      {
        name: name,
        year: year,
        system: system
      }
    end
    game = new_game("Street Figher II", nil, nil, 50)

    Therefore we can use options has argument:

    def new_game(name, options={})
      {
        name: name,
        year: options[:year],
        system: options[:system]
      }
    end
    game = new_game("Street Figher II",
      year: 1992,
      system: "SNES")

    Exception

    def get_tweet(list)
        unless list.authorized?(@user)
            raise AuthorizationException.new
        end
        list.tweets
    end
    
    #raise an Exception instead
    
    begin     
        tweets = get_tweets(my_list)
    rescue AuthorizationException
        warn "You are not authorized to access this list"
    end
  • 相关阅读:
    ST表学习笔记
    LCA学习笔记
    $ZOJ 2432 Greatest Common Increasing Subsequence$
    $SP15637 GNYR04H - Mr Youngs Picture Permutations$
    Noip2016换教室
    洛谷4718【模板】Pollard-Rho算法
    CQOI2016 密钥破解
    Poj3696 The Lukiest Number
    Noip2012同余方程
    同余
  • 原文地址:https://www.cnblogs.com/Answer1215/p/3980544.html
Copyright © 2011-2022 走看看