General newbie qustion

Been working on Ruby for a few weeks now and am well on my way. I've
been working on tutorials and something came up that is never (or hasn't
been explained.

I've seen this used interchangeably so I need to know what this is:

"print x" versus "print "#{x}" ......what the heck is "#{x}"?

If the hash tag wasn't used so much in these languages, my guess would
be "convert x to a number" but that can't be right........though I think
it has some effect like "?" and"!" do

···

--
Posted via http://www.ruby-forum.com/.

Put simply:

"print x" will print just the letter x.
"print #{x}" parses out whatever the variable x is.

···

--
Posted via http://www.ruby-forum.com/.

Ah, apologies. Guess I wrote without thinking hard enough.

···

--
Posted via http://www.ruby-forum.com/.

I've seen this used interchangeably so I need to know what this is:

"print

You really don't see print() too much in ruby. Instead, you more often
see puts() or p(), which is shorthand for inspect(). Here is one
difference between puts() and p():

data = [1, 2, 3]
puts data
p data

--output:--
1
2
3
[1, 2, 3]

···

--
Posted via http://www.ruby-forum.com/\.

Douglas Seifert On Sat, Sep 29, 2012 at 1:41 PM

"print x" will convert whatever object x refers to a string and print it
to the console.

Seems confusing.

x = 7

"print x"
#=>print x
print x
#=>7
print 'x'
#=>x
print "x"
#=>x
print '#{x}'
#=>#{x}
print "#{x}"
#=>7

Simple quotes means literal.
With double quotes Ruby make some more work than simple quotes, it
evaluate whatever code inside #{} giving a result, also transform this
kind of things:

\b \c \n \s #backspaces, reverses, newline, etc.

Well hope that this will help you to understand the difference.

···

--
Posted via http://www.ruby-forum.com/\.

Justin T. wrote in post #1078043:

Put simply:

"print x" will print just the letter x.
"print #{x}" parses out whatever the variable x is.

Ah that makes sense. Much appreciate the help. Unfortunately, the docs
are of little halp. This must be buried deep in them.

Again, thanks

···

--
Posted via http://www.ruby-forum.com/\.

The above is incorrect.

"print x" will convert whatever object x refers to a string and print it to
the console. If x is already referring to a string, no conversion is done.
'print "#{x}"' will create a new string and interpolate into it the object
to which x refers converted into a string. This always creates a new
string, even if x is already referring to a string.

If you see 'print "#{x}"', it might be a code smell, especially if x is
referring to a string, because might be creating a useless copy of an
existing string just to print it out.

Interpolation is better used when you really do need to create a new
string, for example:

print "My variable is '#{x}'"

There are various complex rules on how objects get converted to strings
which you should read up on.

-Doug

···

On Sat, Sep 29, 2012 at 1:41 PM, Justin T. <lists@ruby-forum.com> wrote:

Put simply:

"print x" will print just the letter x.
"print #{x}" parses out whatever the variable x is.

I guess I should have written:

   "print x" (without the quotes)

???

The internet is funny...

-Doug

···

On Mon, Oct 1, 2012 at 9:50 AM, Damián M. González <lists@ruby-forum.com>wrote:

Douglas Seifert On Sat, Sep 29, 2012 at 1:41 PM
>"print x" will convert whatever object x refers to a string and print it
>to the console.

Seems confusing.

It’s not correct.

x = 6
print x

This will print out the number 6, which is the current value of x. To
get the letter x, you want

print "x"

. That being said, #{} simply is a so-called string interpolation. These
two statements are basically equal:

str = "x is " + x.to_s + "!"
str = "x is #{x}!"

There are some subtle differences between them, namely regarding the
number of string objects created. It is generally considered good
coding practive using string interpolation if you don’t have a reason
to do otherwise. Note that #{} can perfectly contain any valid Ruby
expression; if you want to push it to its limits, you can even define a
class inside it. Whatever the expression returns, will be converted to
a string using the #to_s method (which in the case of strings happens
to return the string itself). In contrast to this, the plus expression
wants to use its argument as-is and won’t try to call #to_s on it.
Compare:

x = 6
str = "x is #{x}"
str = "x is " + x

While the former will work as expected (calling Fixnum#to_s, which
returns the string "6"), the latter crashes with a TypeError, because
you tried to combine a number with a string. Chaining is not the same
as interpolation.

Vale,
Marvin

···

Am Sun, 30 Sep 2012 05:44:58 +0900 schrieb George Spak <lists@ruby-forum.com>:

Justin T. wrote in post #1078043:
> Put simply:
>
> "print x" will print just the letter x.
> "print #{x}" parses out whatever the variable x is.

Ah that makes sense. Much appreciate the help. Unfortunately, the
docs are of little halp. This must be buried deep in them.

--
Blog: http://pegasus-alpha.eu/blog

ASCII-Ribbon-Kampagne () | ASCII Ribbon Campaign ()
- Stoppt HTML-E-Mail /\ | - Against HTML E-Mail /\
- Stoppt proprietäre Anhänge | - Against proprietary attachments
www.asciiribbon.org/index-de.html | www.asciiribbon.org