Apply Today to our Front End or Back End Programs & check out our scholarships!

Ruby Review

Back to Curriculum Index

Goals

  • Use the repl.it interface comfortably
  • Identify different between puts and print
  • Explore, then explain what String methods can help us do

Review what we know, puts vs. print

Using the repl.it we just forked, complete the activity that follows:

Try It: Exploring puts vs. print

Read the Ruby code in the repl.it and predict what will happen when it is run. With the concepts and vocabulary you learned in the prework, try to explain why! Be ready to share out.

Now, run the repl.it.

Last, change both instances of puts to print. Observe the change in output. What does that tell you about the job that each command has?

Takeaways

  • print and puts are both classified as commands that are built into the Ruby language.
  • print will print each value it is instructed to. print statments written on different lines of code will not appear on different lines in the output.
  • puts will also print each value it is instructed to; but it will create line breaks between each puts statement.


String Methods

In the spirit of exploring to learn, we aren’t gong to tell what what exactly the code below does - not yet.

first_name = "osCAr"
puts "Hello, #{first_name}!"
puts "Hello, #{first_name.capitalize}!"

Try It: Exploring Methods

Read the code above and predict what will happen when it is run. Try to explain why.

Now, type or copy and paste the code into your repl.it and run it - does that verify or falsify your prediction?

Then, change capitalize to upcase, then downcase, then reverse. Re-run the code each time to change it, and observe the output.

Takeaways

  • Ruby provides a variety of methods that can be used specifically on Strings - we can think of them as actions!
  • A Ruby developer doesn't need to memorize every method that is available; some will be used regularly but others won't. For this reason, resources like Ruby-Doc.org are heavily relied upon by developers!


Next: User Input