Changes between Version 3 and Version 4 of SummaryRuby


Ignore:
Timestamp:
Nov 15, 2014, 12:54:33 PM (10 years ago)
Author:
Thanatermesis
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • SummaryRuby

    v3 v4  
    116116
    117117# Inheritance:
    118 # create a superclass called Doctro from the Person's class (inheritance)
     118# create a superclass called Doctor from the Person's class (inheritance)
    119119class Doctor < Person
    120120    # we have already defined the method "name" in the Person's class, so we extend it:
     
    279279        color = "green"
    280280    else
    281         color "unknown"
     281        color = "unknown"
    282282end
    283283
     
    314314#
    315315a = [1, 2, 3, "four"]
    316 a << 5
    317 a.push 6
     316a << 5      # append an item
     317a.push 6    # append an item
    318318puts a.empty? # false
    319319puts a.include?("four") # true
     
    346346# the delimiter of "each" can be modified passing an argument like .each(',') or
    347347# changing the input delimiter variable $/
     348#
     349# More exactly, yield runs "in the inverse way" including its parameters, what you pass to a call to the function that includes a yield, this is a more clear example:
     350def test
     351   yield 5
     352   puts "method test"
     353   yield 100
     354end
     355test {|i| puts "You are in the block #{i}"}
     356
     357# so you got:
     358# You are in the block 5
     359# method test
     360# You are in the block 100
     361
    348362
    349363
     
    357371        a = (10 / 0)  # if this doesn't works...
    358372    rescue => e
    359         a = 10  # let's still use the original value in failed cases, instead of exit/error
    360         puts "class of exception is: " + e.class.to_s
     373        a = 10  # instead of just exiting we can simply assign a default behaviour and continue
     374        puts "class of exception is: " + e.class.to_s  # show some debug reason of our error
    361375    end
    362376end
     
    595609File.open("text.txt", "w") do |entry| entry.puts "Hi" end
    596610# note that the next line is invalid, if you put a space before ( then is not an argument list what you pass to it
    597 #File.open ("text.txt", "w") do |entry| entry.puts "Hi" end
     611#File.open      ("text.txt", "w") do |entry| entry.puts "Hi" end
    598612
    599613# appending