O'Reilly logo

Rails Crash Course by Anthony Lewis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Appendix A. Solutions

Chapter 1

  1. Exercise 1 is about learning to read a file and exploring the array methods using the contents of the file. I’d expect to see something like this in the console after completing the exercise:

    irb(main):001:0> file = File.read("test.txt")
     => "Call me Ishmael..."
    irb(main):002:0> puts file.split
    Call
    me
    Ishmael
    --snip--
     => nil
    irb(main):003:0> puts file.split.length
     => 198
    irb(main):004:0> puts file.split.uniq.length
     => 140

    The output depends on the text you used.

  2. The second exercise requires writing a little code. The following sample solves the problem using only methods covered so far:

    file = File.read("test.txt") counts = {} file.split.each do |word| if counts[word] counts[word] = counts[word] + 1 else counts[word] ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required