How would you call each method on a separate line? How would it look?


#1

Can you show me how it would look to call each method on a separate line?


#2

Hey jkm345

I'm assuming you're talking about the 15th exercise (Strings and String Methods), since you didn't mention specifically but it talks about running commands on different lines or chaining them together.
I've written a sample script below that explains how it would look like as chained methods, methods called on multiple lines, and as a bonus, self-modified methods called on multiple lines (will learn in next exercise, but it fits with your question so I put it in)

  #Example of chaining methods together
  #This works because each method returns a string which we can execute the next method on
  name = "MyRandomString".downcase.reverse.upcase

  puts name #Will output "GNIRTSMODNARYM"

  #Doing this on multiple lines, would look like this
  #Here we re-assign the "name" variable to the result string given by the method call

  name = "MyRandomString"
  name = name.downcase
  name = name.reverse
  name = name.upcase

  puts name #Will also output "GNIRTSMODNARYM"


  #Another way to do the multiple lines example, which you will learn in the next lession, is to add a "!" to the end of the method name
  #This will modify the original string instead of returning a new one, so we can skip having to re-assign the "name" variable to the return value
  #Note: This doesn't work on every method, but is common for methods that return an object of same class that is being called (Such as String.downcase returns a string object)

  name = "MyRandomString"
  name.downcase!
  name.reverse!
  name.upcase!

  puts name #This will also output "GNIRTSMODNARYM"