Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Ruby Basics!
You have completed Ruby Basics!
Preview
You can join strings together using string concatenation.
IRB
- IRB stands for "Interactive RuBy".
- You can run it by clicking in your console or terminal and typing
irbat the system prompt. - When we run it, it'll show you a prompt where you can type Ruby expressions one at a time, hitting Enter after each.
- IRB will immediately show you the result of each expression. You don't need to call
putsor anything.
2.3.0 :001 > 1 + 2
=> 3
2.3.0 :002 > Time.now
=> 2017-09-02 13:31:38 -0700
- When you're done and you're ready to exit IRB, type
exitand press Enter. You'll be returned to the system prompt.
2.3.0 :003 > exit
$
- IRB is a great way to try code out and see what it does, and even professional Ruby developers use it as a way to quickly test out ideas.
String Concatenation
So now that we know how irb works, let's use it to try out string concatenation.
$ irb
2.3.0 :001 > "a" + "b"
=> "ab"
2.3.0 :002 > "some words" + "more words"
=> "some wordsmore words"
2.3.0 :003 > "some words" + " " + "more words"
=> "some words more words"
2.3.0 :004 > myvar = "a string"
=> "a string"
- You can concatenate strings in variables
2.3.0 :005 > myvar + " abc"
=> "a string abc"
- Concatenation gives a new string, it doesn't change the string in the variable
2.3.0 :006 > myvar
=> "a string"
- To change the variable's value, use an abbreviated assignment operator, which we'll talk more about soon
myva2.3.0 :007 > myvar += " abc"
=> "a string abc"
2.3.0 :008 > myvar
=> "a string abc"
myva2.3.0 :009 > myvar += " def"
=> "a string abc def"
myvar
2.3.0 :010 > myvar
=> "a string abc def"
- Strings can only be concatenated together with other strings. Anything else, like a number, will result in an error.
- We'll be showing you a solution for this shortly.
2.3.0 :001 > 1 + "a string"
TypeError: String can't be coerced into Fixnum
from (irb):1:in `+'
from (irb):1
from /Users/jay/.rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'
2.3.0 :002 > "a string" + 1
TypeError: no implicit conversion of Fixnum into String
from (irb):2:in `+'
from (irb):2
from /Users/jay/.rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'
Updating the widget store
- Using string concatenation to fix our
askmethod- We need to print a space following the question we ask the user
- We can do this using string concatenation
def ask(question)
print question + " "
gets
end
puts "Welcome to the widget store!"
answer = ask("How many widgets are you ordering?")
- Let's print what the user entered so they can confirm it's correct.
answer = ask("How many widgets are you ordering?")
puts "You entered" + answer + "widgets"
- Output:
You entered11
widgets
- Oops! We need to add spaces surrounding
answer, so fix that:puts "You entered " + answer + " widgets"- Output:
You entered 11
widgets
- You may be wondering why we didn't get an error, since strings can only be concatenated with other strings. The reason is, the value in the
answervariable is a string. Thegetsmethod always returns strings. So even though the user entered a number, it's treated as a string. Eventually we'll have to convert it to an actual number, which we'll see how to do later. - It still skips to a new line after printing
answer. That's something we'll have to fix later as well.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
In order to fix this, we're going
to need to take the question that
0:00
we're asking the user, and
add a space on to the end of it.
0:01
We can do this through ruby string
concatenation or the joining of strings.
0:04
We'll show you how to do string
concatenation in our main program
0:09
in a bit, but
first let's try it out in a different way.
0:13
I want to show you a separate program that
gets installed along with Ruby called irb.
0:16
irb stands for interactive Ruby and we can
launch it by clicking down in our console
0:22
and typing the letters irb and
pressing Enter.
0:27
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up