20 minutes Ruby

Source: Internet
Author: User

ref:https://www.ruby-lang.org/en/documentation/quickstart/

#!/usr/bin/env rubyclass 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 "..." elsif @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 # Say bye to everybody def Say_bye if @names. Nil? Puts "..." elsif @names. respond_to?  ("join") # Join the list elements with commas puts "Goodbye #{@names. Join (", ")}.    Come back soon! "  Else puts "Goodbye #{@names}.    Come back soon! "  End Endendif __file__ = = $ mg = megagreeter.new Mg.say_hi Mg.say_bye # change name to be "Zeke" mg.names = "Zeke" Mg.say_hi Mg.say_bye # Change the name to an array of names mg.names = ["Albert", "Brenda", "Charles", "Da VE "," Engelbert "] mg.say_hi Mg.say_bye # change to nil mg.names = nil Mg.say_hi mG.say_byeend 

1. Attr_accseeor:name

The return value (Array) for megagreeter.instance_methods false is many: id:id= two items

Mg.respond_to? : ID or mg.respond_to? (" ID ")

mg.respond_to?: id= or mg.respond_to? (" Id= ") is True

2. Use the object's properties instead of the object's type to determine how to execute

@names. respond_to?

3. __file__==$0

20 minutes Ruby

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.