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