📄 12 - calling a superclass's method.rb
字号:
class Recipe # ... The rest of the Recipe implementation goes here. def cook(stove, cooking_time) dish = prepare_ingredients stove << dish wait_for(cooking_time) return dish endend#---class RecipeWithExtraGarlic < Recipe def cook(stove, cooking_time) 5.times { add_ingredient(Garlic.new.chop) } super endend#---class BakingRecipe < Recipe def cook(cooking_time, oven_temperature=350) oven = Oven.new(oven_temperature) super(oven, cooking_time) endend#---class MyString < String def gsub(*args) return "#{super} -- This string modified by MyString#gsub (TM)" endendstr = MyString.new("Here's my string")str.gsub("my", "a")# => "Here's a string -- This string modified by MyString#gsub (TM)"str.gsub(/m| s/) { |match| match.strip.capitalize }# => "Here's MyString -- This string modified by MyString#gsub (TM)"#---class MyString def succ!(skip=1) skip.times { super() } self endendstr = MyString.new('a')str.succ!(3) # => "d"#---class MyFile < File def MyFile.ftype(*args) return "The type is #{super}." endendFile.ftype("/bin") # => "directory"MyFile.ftype("/bin") # => "The type is directory."```#---
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -