Advanced Ruby Building Blocks

Introduction

This lesson gets more into the topics you may have been shaky on before like Control Flow, Looping, Arrays, Hashes, Blocks, Sorting. It’s definitely got some more meat to it than the previous one. The projects in the Assignment section are good for doing on your own because Codecademy gives you all kinds of help with them.

Learning Outcomes

Look through these now and then use them to test yourself after doing the assignment:

Conditionals and Flow Control:

  • What is a “boolean”?
  • What are “truthy” values?
  • Are nil, 0, "0", "", 1, [], {} and -1 considered true or false?
  • When do you use elsif?
  • When do you use unless?
  • What does <=> do?
  • Why might you define your own <=> method?
  • What do || and && and ! do?
  • What is returned by puts("woah") || true?
  • What is ||=?
  • What is the ternary operator?
  • When should you use a case statement?

Iteration:

  • What does loop do?
  • What are the two ways to denote a block of code?
  • What is an index variable?
  • How do you print out each item of a simple array [1,3,5,7] with:
    • loop?
    • while?
    • for?
    • #each?
    • #times?
  • What’s the difference between while and until?
  • How do you stop a loop?
  • How do you skip to the next iteration of a loop?
  • How would you start the loop over again?
  • What are the (basic) differences between situations when you would use while vs #times vs #each?
  • What does nesting loops mean and when would you use it?

Blocks, Procs, and Lambdas:

  • How is a block like a function?
  • How is a block different from a function?
  • What are the two ways to declare a block?
  • How do you return data from a block?
  • What happens if you include a return statement in a block?
  • Why would you use a block instead of just creating a method?
  • What does yield do?
  • How do you pass arguments to a block from within a method?
  • How do you check whether a block was actually passed in?
  • What is a proc?
  • What’s the difference between a proc and a block?
  • When would you use a proc instead of a block?
  • What is a closure?
  • What is a lambda?
  • What’s different between a lambda and a proc?
  • What is a Method (capital “M”)?
  • What do Methods basically allow you to do that could probably be pretty interesting when you’re writing some more advanced programs later on?

Enumerable and Modules:

  • What is a module?
  • Why are modules useful?
  • What does #each do?
  • What does #each return?
  • What does #map do?
  • What does #map return?
  • What is the difference between #map and #collect?
  • What does #select do?
  • What does #select return?
  • What is the difference between #each #map and #select?
  • What does #inject do?
  • When might you use #inject?
  • How do you check if every item in a hash fulfills a certain criteria?
  • What about if none of the elements fulfill that criteria?
  • What (basically) is an enumerator?

Writing Methods:

  • How many things should a method ideally do?
  • What types of things should a method modify?
  • What should you name a method?
  • What does self mean?
  • What do you need to do to create your own Ruby script file?
  • How would you run a Ruby script from the command line?
  • How can you check whether your script was being run from the command line?
  • What is a shebang line?
  • What does require do?
  • What does load do?
  • What is the difference between require and load?
  • How do you access any parameters that were passed to your script file from the command line?
  • What does #send do?
  • When would #send be used that’s different from just running the method on an object ‘normally’?

Assignment

  1. Do the Codecademy Ruby sections 2-8, including:
    1. Control Flow in Ruby
    2. Project: Thith Meanth War!
    3. Looping with Ruby
    4. Project: Redacted!
    5. Arrays and Hashes
    6. Blocks and Sorting
    7. Project: Ordering your Library
    8. Hashes and Symbols
    9. Project: A Night at the Movies
    10. The Zen of Ruby
    11. The Refactor Factory
    12. Blocks, Procs, and Lambdas
  2. Finish Beginning Ruby Chapter 3: Ruby's Building Blocks: Data, Expressions, and Flow Control (pages 47-71)
  3. For a look at underserved concepts and help with the questions above, check out these posts by Erik Trautman:
    1. Ruby Explained: Conditionals and Flow Control
    2. Ruby Explained: Iteration
    3. Ruby Explained: Blocks, Procs, and Lambdas, aka “Closures”
    4. Ruby Explained: Map, Select, and Other Enumerable Methods
    5. Ruby Explained: Writing and Running Methods

Test Yourself

Make sure you can do the following quizzes from Code Quizzes. They’re pretty quick and should give you an idea of what you still need to brush up on.

  1. Beginner Ruby Quiz #2
  2. Quiz #3
  3. Quiz #4
  4. Quiz #6
  5. Make sure you go back up and look at all the questions from the “Points to Ponder” section. See if you can do most of them without looking back through the text.

Additional Resources

This section contains helpful links to other content. It isn’t required, so consider it supplemental for if you need to dive deeper into something.

Ruby Programming

Advanced Ruby Building Blocks

Got questions?

Chat with our friendly Odin community in our Gitter chatrooms!

Open Gitter

Are you interested in a more immersive web development learning experince?

Learn more

Viking Code School

  • 12 weeks

    12 Weeks

  • Fully Online

    Fully Online

  • Mentorship

    Mentorship