Changes between Version 3 and Version 4 of SummaryRuby
- Timestamp:
- Nov 15, 2014, 12:54:33 PM (10 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
SummaryRuby
v3 v4 116 116 117 117 # Inheritance: 118 # create a superclass called Doct rofrom the Person's class (inheritance)118 # create a superclass called Doctor from the Person's class (inheritance) 119 119 class Doctor < Person 120 120 # we have already defined the method "name" in the Person's class, so we extend it: … … 279 279 color = "green" 280 280 else 281 color "unknown"281 color = "unknown" 282 282 end 283 283 … … 314 314 # 315 315 a = [1, 2, 3, "four"] 316 a << 5 317 a.push 6 316 a << 5 # append an item 317 a.push 6 # append an item 318 318 puts a.empty? # false 319 319 puts a.include?("four") # true … … 346 346 # the delimiter of "each" can be modified passing an argument like .each(',') or 347 347 # 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: 350 def test 351 yield 5 352 puts "method test" 353 yield 100 354 end 355 test {|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 348 362 349 363 … … 357 371 a = (10 / 0) # if this doesn't works... 358 372 rescue => e 359 a = 10 # let's still use the original value in failed cases, instead of exit/error360 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 361 375 end 362 376 end … … 595 609 File.open("text.txt", "w") do |entry| entry.puts "Hi" end 596 610 # 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" end611 #File.open ("text.txt", "w") do |entry| entry.puts "Hi" end 598 612 599 613 # appending