"Java Data Structures - Retired" was retired on May 31, 2019. You are now viewing the recommended replacement.
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!
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
irb
at 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
puts
or 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
exit
and 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
ask
method- 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
answer
variable is a string. Thegets
method 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
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