Forum: Ruby general newbie qustion

Posted by George Spak (binder_dundat)
on 2012-09-29 22:38
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 by Justin T. (justin_t)
on 2012-09-29 22:41
Put simply:

"print x" will print just the letter x.
"print #{x}" parses out whatever the variable x is.
Posted by George Spak (binder_dundat)
on 2012-09-29 22:44
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 by Douglas Seifert (Guest)
on 2012-09-29 23:17
(Received via mailing list)
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.
>
>
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
Posted by Marvin Gülker (quintus)
on 2012-09-29 23:47
Attachment: signature.asc (489 Bytes)
(Received via mailing list)
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.

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


--
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
Posted by Justin T. (justin_t)
on 2012-09-30 01:30
Ah, apologies. I see my error, guess I wrote without thinking hard 
enough.
Posted by 7stud -- (7stud)
on 2012-09-30 02:21
> 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 by Damián M. González (igorjorobus)
on 2012-10-01 18:50
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 by Douglas Seifert (Guest)
on 2012-10-02 07:08
(Received via mailing list)
On Mon, Oct 1, 2012 at 9:50 AM, Damin M. Gonzlez 
<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.
>

I guess I should have written:

   "print x" (without the quotes)

???

 The internet is funny...

-Doug
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.