[Ruby] Level 2 Methods and Classes

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.