Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialA X
12,842 PointsWhy can't we subtract or divide a string from a string in Ruby?
If we can add and multiply strings in Ruby why can't we subtract or divide a string in Ruby? Granted I'm not sure if I could think of why you'd want to divide a string, but I still can't see why logically this can't be done.
1 Answer
Akshat Jain
12,819 PointsYou aren't adding or multiplying strings, you are concatenating them. Subtracting or Dividing strings doesn't work because it doesn't make sense.
A X
12,842 PointsA X
12,842 PointsAh, this wasn't explained well in the video. I didn't realize before that multiplying strings is still considered a form of concatenation! (I have seen the + sign used for concatenation before) Are there other forms of concatenation of strings outside of * and + ?
A X
12,842 PointsA X
12,842 PointsI also wanted to comment that at least to me subtracting a string from a string does at least seem to make sense... in the video that illustrates my question, Jason concatenates (or looks like he's "adding") his first name "Jason" to his last name "Seifer" and then he attempted to remove his last name from the combination of his first and last name (similar to a delete). I agree though that dividing strings doesn't seem to make any sense :)
Akshat Jain
12,819 PointsAkshat Jain
12,819 PointsThe behaviour is not consistent, suppose you could do
"I Love Treehouse" - "Treehouse"
but then, what do you think should happen when you do
"I Love Treehouse" - "Love"
or even
"I Love Treehouse" - "Random"