class MagGreeter
attr_accessor :name;
def initializer(name ="World")
@name = name;
end
def sayhi if @name.nil? #只有当@name = nil的时候,@name.nil?才为真。 puts"...";
elsif @name.respond_to?("each")
@name.each do |name| puts"hi #{name}";
end
else puts"hi #{@name}";
end
end
def saygoodbye if @name.nil puts"..."
elsif @name.respond_to?("join") puts"goodbye, "+ @name.join(",");
else puts"goodbye #{@name}";
end
end
end if __FILE__ == $0
a = MagGreeter.new;
a.name ="liujun";
a.sayhi;
a.name =["pretty gril",'mike'];
a.sayhi;
a.name = nil;
a.sayhi; puts nil.nil?; puts''.nil?; #=>false,ruby里面,空字符并不是nil对象。
end