0

I can't find the answer using Google.

Works!

i = 15
appended = "Dark " << "Silk"
appended = appended

Doesn't work. :(

i = 15
appended = "Dark " << i
appended = appended
9
  • "doesn't work" is not a proper error description. Commented Jun 25, 2012 at 19:27
  • 1
    appended = "Dark " << i.to_s Commented Jun 25, 2012 at 19:28
  • checkout apidock.com/ruby/v1_9_3_125/String/%3C%3C If the object is a Integer, it is considered as a codepoint, and is converted to a character before concatenation. Commented Jun 25, 2012 at 19:30
  • 1
    appended = appended is redundant. You already set the variable. Commented Jun 25, 2012 at 19:33
  • 1
    Most installations (if not all) come with a book about Ruby. /Ruby192/doc/bookofruby.pdf for example. Commented Jun 25, 2012 at 20:00

3 Answers 3

9

Try this:

i = 15
appended = "Dark " + "Silk"

or for non-String objects:

appended = "Dark " + i.to_s

You can also use string interpolation (which is more idiomatic):

appended = "Dark #{i}"
Sign up to request clarification or add additional context in comments.

2 Comments

Where can I find some good documentations for Ruby? The Ruby-lang official one is ridiculous as I looked up variables and there wasn't much information on it. Is there any useful ones that you keep referring back to, now and again?
Ruby-doc: ruby-doc.org However for things like string interpolation which aren't really something you'd know about without reading other Ruby code, I'd recommend just occasionally reading production Ruby code written by other people to learn tricks like that.
2

Does

"Dark" << i.to_s

do what you want?

2 Comments

The long explanation here is that Ruby does not generally convert types like other languages such as JavaScript do.
Right. For example 1 + '1' is valid in javascript, but yields '11' which might not be what you want. So ruby shouts at you if you try to do it.
2

"Silk" is a string and 15 is an integer. You can ONLY concatenate and string to another string. That's why "Dark" << "Silk" works. If you first transform 15 into a string with 15.to_s, you'll be able to concatenate it.

I suggest you read through Ruby's documentation to find out more about built-in classes and methods.

6 Comments

I looked up the word variable on the page, and the only matches that came up were class_variables methods. How can a documentation not teach me how to set a variable? No wonder I had to ask this question.
That was the languages reference, not a tutorial. Ruby documentation is very clear and helpful. Should you want to learn how to start programming in Ruby, there are a number of tutorials around the internet. Try out Programming Ruby: The Pragmatic Programmer's Guide.
I searched the site and I can find it now. ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html It's just weird how a reference doesn't teach me how to set a variable. Even the C and Processing.js reference teaches me how to do that! This is just ridiculous! I thought about reading the tutorial books on the Ruby language, but instead I've decided to dive right in coding with the Ramaze framework and learn as I go along, like I did before with other languages.
A reference is meant to be used as reference by people who already know the language's basics, not as a tutorial. Besides, what you are trying to do is not set a variable, but call instance methods with parameters, although you probably haven't noticed this.
I must be doing it wrong because I use references as a substitute for tutorials, once I have a small patch of code to start off from. It worked for me in other languages.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.