DEV Community

Cover image for This week I learned...
camille-arce
camille-arce

Posted on • Edited on

2

This week I learned...

Week: 03/05/2023 - 03/11/2023

Working with Strings in Ruby

Input:

'Single quoted strings #{represent} "characters" verbatim.'
Enter fullscreen mode Exit fullscreen mode

Output:

Single quoted strings #{represent} "characters" verbatim.
Enter fullscreen mode Exit fullscreen mode

Input:

"Double-quoted strings make some substitutions: #{2 + 4}"
Enter fullscreen mode Exit fullscreen mode

Output:

Double-quoted strings make some substitutions: 6
Enter fullscreen mode Exit fullscreen mode

To join Strings, you can interpolate or concatenate

  • String concatenation

    • joining of strings
    • can join strings together using "" and + between strings
    • you have to add " ", an empty string with a space to prevent two words being put together
    • myvar can be a variable inserted with a string and can add other strings with the plus sign +
    • if you do += the myvar will be have the new concatenation as the new string
    • strings can only be concantenated with other strings
  • String interpolation

    • interpolation #{} can be used within double quoted strings and it will substitute the result of a ruby code into the middle of the string
    • the interpolation will be evaluated and the result will be converted into a string

Interactive Ruby (irb)

  • can use IRB (interactive ruby) which is integrated in Ruby if you type "irb" and hit return in terminal
  • irb will show you the result of each expression, you dont need to call "puts" or anything
  • type "exit" and then enter to exit out of IRB

p vs puts vs print method

  • Ruby provides a method p which is different than print and puts
  • p method allows us to inspect the values we pass to it and to print them out approximate the same way they would appear in a ruby code

Input: puts

            puts "   "
            puts ""
            puts nil
            puts []
Enter fullscreen mode Exit fullscreen mode

The output is an empty terminal

Input: print

            print "    "
            print ""
            print nil
            print []
Enter fullscreen mode Exit fullscreen mode

The output is []%

Input: p

            p "    "
            p ""
            p nil
            p []
Enter fullscreen mode Exit fullscreen mode

The output is:

            "     "
            ""
            nil
            []
Enter fullscreen mode Exit fullscreen mode

Some escape sequences
- \n skips to a new line
- \t indents a text
- \" inserts "double quotes"
- \' inserts 'single quotes'
- \ inserts a backslash: \

Input: example

        puts "first line
        second line"

        puts "     new paragraph"
        puts "  He said, "Whoa.""
Enter fullscreen mode Exit fullscreen mode

to fix the line above use escape sequences

    puts "first line\nsecond line"
    puts "\tnew paragraph"`
    puts "He said, \"whoah\""
Enter fullscreen mode Exit fullscreen mode

Next Week: Ruby Objects

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more