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 = New_game ("Street figher II")
Options Hash Argument
Sometimes, optinal argumetns also not good. For exmaple, we had one argument added to the end, if we did want pass in reply_id and year, system we don ' t need to pass In, then we need-put placeholder in the function call.
def new_game (name, Year=nil, system=nil, reply_id = nil) { name:name, year:year, system : System = New_game ("Street figher II", nil, nil, 50)
Therefore We can use options have argument:
def new_game (name, options={}) { name:name, year:options[:year], system: Options[:system] = New_game ("Street figher II", 1992 , " SNES ")
Exception
def get_tweet (list) unless list.authorized? ( @user) raise authorizationexception.new end list.tweetsend# Raise an Exception insteadbegin = get_tweets (my_list) Rescue Authorizationexception "You is not a authorized to access the Thislist"end
[Ruby] Level 2 Methods and Classes