Learning Ruby by Michael Fitzgerald The unconfirmed error reports are from readers. They have not yet been approved or disproved by the author or editor and represent solely the opinion of the reader. Here's a key to the markup: [page-number]: serious technical mistake {page-number}: minor technical mistake : important language/formatting problem (page-number): language change or minor formatting problem ?page-number?: reader question or request for clarification This page was updated June 6, 2008. UNCONFIRMED errors and comments from readers: {23} last line of text on page; http://www.oreilly.com/catalog/learningruby gives a 404 error (27) fourth paragraph; Here is another form. This block comment conceals several lines from the interpreter with =begin/=end: The correction should be "Here is another form. This block comment conceals several lines from the interpreter with =begin/=end (without any leading spaces or tabs:" This is not a serious problem but an inadvertent space or tab will create an error msg. [48] 1st paragraph; There is an extra "END" on this IF statement while the previous sentence clearly tries to explain that the keyword "END" is mandatory. " In addition, you don't have to use end if you write this code all on one line, like so: x - 256 if x == 256 then puts "x equals 256" end " (50) 1st paragraph; In reference to an if / else construct, the text reads: The 'else' keyword gives 'if' an escape hatch. In other words, if the 'if' statement does not evaluate true, the code after 'else' will be executed, and if 'if' evaluates 'false', the code after 'else' is ignored. Should probably be something like: The 'else' keyword gives 'if' an escape hatch. In other words, if the 'if' statement does not evaluate true, the code after 'else' will be executed, and if 'if' evaluates 'true', (the code after 'if' is executed and) the code after 'else' is ignored. {53} 5th paragraph (beginning "Also, like if..."; irb(main):001:0> cash = 100_000.00 => 100000.0 irb(main):002:0> sum = 0 => 0 irb(main):003:0> cash += 1.00, sum while cash < 1_000_000.00 SyntaxError: compile error (irb):3: parse error, unexpected ',', expecting $ cash += 1.00, sum while cash < 1_000_000.00 ^ from (irb):3 # I get an error using the comma, using a semicolon instead, and not using anything. # Using # irb 0.9.5(05/04/13) # ruby 1.8.4 (2005-12-24) [powerpc-darwin8.7.0] {65} 5th para, 1st bullet; quote: hay == nicolay # => false ... ...You could also apply the eql? method and get the same results, though eql? and == are slightly different: [bullet] == returns true if two objects are Strings, false otherwise end quote. The first bullet is wrong as your own example "hay == nicolay" shows. See corelib.ruby, which defines == like this: "Equality?If obj is not a String, returns false. Otherwise, returns true if str <=> obj returns zero." Also your examples don't show any difference between "==" and ".eql?", since "hay.eql?" also returns false. (68) last code sample of section 4.5.4 - The delete method; This applies to the Safari online version of the book as of May 13, 2008. The example code doesn't generate the given output: "That's all folks".delete "abcdefghijklmnopqrstuvwxyz", "^ha" # => "haa" The .delete method will only delete the lowercase letters, excepting "h" and "a". It will not delete the uppercase letters ("T") or the punctuation (space and single quote), so the result is "Tha' a ", not "haa". {75} Last code snippet; In all of the phone.grep examples (starting on p75, and continuing to p76), the area code is incorrectly enclosed in square brackets, rather than parentheses. That example should read: phone.grep(/(\(\d\d\d\))?\d\d\d-\d\d\d\d/) # => ["(555)123-4567"] The examples will produce the output shown, because the area code segment is made optional with "?". The remainder of the expression matches the remainder of the phone number ("123-4567"), and so an array consisting of the phone string is returned, as shown. But the text implies that the regular expressions shown match the ENTIRE number, which is not true. {80} Figure 5-1; The diagram has quite a few errors: - Rational is a child of Object, not Integer - Complex is a child of Numeric, not Integer - Float is a child of Numeric, and missing from the diagram {82} 2nd block of command line examples; The example "12.quo 5" does not return 2.4, it returns Rational(12, 5) To obtain that the example should be "12.0.quo 5.0" [96] After 1st paragraph; irb(main):008:0> digits = Array(0..9) => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] not [1, 2, 3, 4, 5, 6, 7, 8, 9] {102} 2nd code example; months.insert(0, nil) does not replace "nil," insert() adds nil so the array looks like this: [nil, "nil", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] (116) last two line on the page; I believe that... dir.tell #=> "." dir.read #=> 1 should be switched to... dir.tell #=> 1 dir.read #=> "." (119) middle of page; "run the program with these three files on the command line:" # argf.rb sonnet_29.txt sonnet_119.txt sonnet_129.txt should be ruby argf.rb sonnet_29.txt sonnet_119.txt sonnet_129.txt (120) top of page; link = page_content.scan(/0?r_1:1; r2 = r_2>0?r_2:6, is incorrect in that in over one million rolls a 12 is never obtained. While such results are not impossible, statistically they qre highly unlikely. Re-writing the statements as r1 = r_1>0?r_1:6; r2 = r_2>0?r_2:6, that is setting both r1 and r2 to 6 in the negative case solves the problem. Data follows: with r1 = r_1>0?r_1:6 1000000 rolls Frequencies Actual Expected two = 27909 0.028 0.028 three = 55540 0.055 0.056 four = 83132 0.083 0.083 five = 111112 0.111 0.111 six = 139029 0.139 0.139 seven = 166741 0.168 0.167 eight = 139193 0.139 0.139 nine = 110788 0.111 0.111 ten = 83264 0.083 0.083 eleven = 55388 0.054 0.056 twelve = 27904 0.028 0.028 with r1 = r_1>0?r_1:1 1000000 rolls Frequencies Actual Expected two = 55501 0.056 0.028 three = 83259 0.083 0.056 four = 111443 0.111 0.083 five = 139386 0.139 0.111 six = 166769 0.167 0.139 seven = 166207 0.166 0.167 eight = 110925 0.111 0.139 nine = 83170 0.083 0.111 ten = 55552 0.056 0.083 eleven = 27788 0.028 0.056 twelve = 0 0.000 0.028 [135] Example 19.2; the method roll does not generate dice rolls from 2 to 12. it has more than 1 error. Results of a run of 3000 executions are shown 2 is generated 167 times, 11 generated 93 times, 12 never generated. Test code provided below results. 2 167 3 271 4 325 5 403 6 514 7 487 8 369 9 231 10 140 11 93 Run code below and see what happens when the three methods are plugged into the main routine and are called: #!C:\Rails\InstantRails\ruby\bin\ruby.exe -w # Written by: David S. Banham # Version : 1.0 # Create Test-case Count TEST_CASES = 3000 # Create a hash representing the possible dice outcomes and initialize counts to 0 $roll=Hash.new(0) # Three dice algorithms # simplified version of algorithm on page 135 def roll_dice_lp_simple r_1 = rand(6) r1 = r_1>0?r_1:1 r1 end # algorithm on page 135 def roll_dice_lp r_1 = rand(6); r_2 = rand(6) r1 = r_1>0?r_1:1; r2 = r_2>0?r_2:6 total = r1+r2 # puts "you rolled #{total}" total end # correct simple dice roll algorithm def roll_dice rand(max=6) + 1 end # Main Program plug in any algorithm and look at its results for # a large number of rolls of the dice puts Time.now.to_f for i in 1..TEST_CASES dice_value = roll_dice_lp # puts dice_value $roll[dice_value] = $roll[dice_value] +1 end puts Time.now.to_f puts"\n\n\n" puts $roll.sort puts "\n\nThe End" # End of Main Program [152] middle; "a hint that Ruby is managing resources efficiently"... NOT TRUE. if you say a = b a.object_id and b.object_id all the time... a and b point to the same object. hence same object id. (156) middle of page; Program example asks to load from http://www.wyeast.net/images/sunrise.jpg site does not exist [190] last paragraph; After following successfully all previous instructions ./script/generate scaffold address address gives: 17:29:07 -> ./script/generate scaffold address address exists app/controllers/ exists app/helpers/ create app/views/address exists app/views/layouts/ exists test/functional/ dependency model exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/address.rb create test/unit/address_test.rb create test/fixtures/addresses.yml error Before updating scaffolding from new DB schema, try creating a table for your model (Address) 17:29:20 -> instead of what the book reports.