Ruby Study Notes 0708

Source: Internet
Author: User
#! /Usr/bin/ENV rubyclass megagreeter attr_accessor: names # initialize this object def initialize (names = "world ") @ names = names end # Say hi def say_hi if @ names to everyone. nil? Puts "..." elsif @ names. respond_to? ("Each") # @ names is an iterator column container that can be iterated @ names. Each do | Name | puts "hello # {name }! "End else puts" hello # {@ names }! "End # Tell Everyone bye def say_bye if @ names. nil? Puts "..." elsif @ names. respond_to? ("Join") # concatenate the elements in the orders column into a string of puts "goodbye # {@ names. Join (", ")}. Come back soon! "Else puts" goodbye # {@ names}. Come back soon! "End endendif _ file _ = $0 mg = megagreeter. new mg. say_hi mg. say_bye # changed to "Zeke" mg. names = "Zeke" mg. say_hi mg. say_bye # change to a named mg column. names = ["Albert", "Brenda", "Charles", "Dave", "Engelbert"] mg. say_hi mg. say_bye # changed to nil mg. names = nil mg. say_hi mg. say_byeend

This short code is the code that Ruby beginners will be familiar with. For details, refer to 20 minutes to learn Ruby.

Japanese link https://www.ruby-lang.org/zh_tw/documentation/quickstart/4/

Chinese link http://tech.it168.com/d/2007-09-04/200709041111532.shtml

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.