#! /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