Ruby journey (1) Ruby journey Step 1 FIRST MEETING

Source: Internet
Author: User

When I started my ruby journey, I wrote my study notes for people with other OO language experiences. Let's take a look at the magic ruby.

The first sample! First meeting.

# ! /Usr/bin/ENV Ruby

Class Megagreeter
Attr_accessor: names

# Create the object
Def Initialize (names =   " World " )
@ Names = Names
End

# Say hi to everybody
Def Say_hi
If @ Names. nil?
Puts " "
Else
If @ Names. respond_to? ( " Each " )

# @ Names is a list of some kind, iterate!
@ Names. Each do | Name |
Puts " Hello # {name }! "
End
Else
Puts " Hello # {@ names }! "
End
End
End

# Say bye to everybody
Def Say_bye
If @ Names. nil?
Puts " "
Else
If @ Names. respond_to? ( " Join " )

# @ Join the list elements with commas
Puts " Goodbye # {@ names. Join ( " , " )}. Come back soon! "
Else
Puts " Goodbye # {@ names }! "
End
End
End

End

If   _ File __   = $0
MG = Megagreeter. New
MG. say_hi
MG. say_bye

# Change name to be "Zeke"
MG. Names =   " Jaylee "
MG. say_hi
MG. say_bye

# Change the name an array of names
MG. Names = [ " Albert " , " Brenda " , " Coderlee " ]
MG. say_hi
MG. say_bye

# Change to nil
MG. Names = Nil
MG. say_hi
MG. say_bye
End

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.