String Interpolation of Compound Statement

Hi folks. I come from a language where we don’t have string interpolation. Here’s what I would usually write:

@f += "\t<label for=\"" + @h[("s_fieldname_" + i.to_s)].to_s + "\">" + @fieldname.capitalize + “:"

Now I’ve been told there are less errors with string interpolation. How can I translate that? I get hung up on the internal bit here:

@h[("s_fieldname_" + i.to_s)

I’m sure it’s out there. I appreciate any input.

Cheers, Bee

Hi folks. I come from a language where we don’t have string interpolation.
Here’s what I would usually write:

@f += "\t<label for=\"" + @h[("s_fieldname_" + i.to_s)].to_s + "\">" +
@fieldname.capitalize + “:"

Without wanting to confuse you too much, I would probably do something like this:

    @f += %Q|\t<label for="%s">%s:| % [ @h["s_fieldname_#{i}"].to_s, @fieldname.capitalize ]

The % method substitutes the values in the array for successive %s in the string. Add in the %Q trick, and as you can see, the expression is a lot easier on the eyes...

Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt;

2.2.1 :005 > @f = "<html>"
=> "<html>"
2.2.1 :006 > @h = {"s_fieldname_1" => "username"}
=> {"s_fieldname_1"=>"username"}
2.2.1 :007 > @fieldname = "username"
=> "username"
2.2.1 :008 > i = 1
2.2.1 :009 > %Q{#{@f}\t<label
for="#{@h["s_fieldname_#{i.to_s}"]}">#{@fieldname.capitalize}:}
=> "<html>\t<label for=\"username\">Username:"

I'm using %Q{} so that you can more easily use " inside. If you don't
have " as a character in your string, I tend to use " as my string
literal delimiter. For example:

2.2.1 :010 > "hello, my name is #{@fieldname}"
=> "hello, my name is username"

Also note that for special variables such as class instance variables
(@xxxx) and global variables ($xxxx) you can omit the {}:

2.2.1 :011 > "hello, my name is #@fieldname"
=> "hello, my name is username"

But this is a shortcut I don't like too much, I prefer to always have
the {} to delimit interpolated pieces.

Regarding the nested interpolations, just remember that inside #{} you
have pure Ruby without knowledge of the surrounding quotes or anything
else, so you don't need to escape ", for example:

2.2.1 :017 > "hello, my name is #{[1,2,3].join("-")}"
=> "hello, my name is 1-2-3"

Hope this helps,

Jesus.

···

On Fri, Sep 2, 2016 at 4:08 PM, Bee.Lists <bee.lists@gmail.com> wrote:

Hi folks. I come from a language where we don’t have string interpolation. Here’s what I would usually write:

@f += "\t<label for=\"" + @h[("s_fieldname_" + i.to_s)].to_s + "\">" + @fieldname.capitalize + “:"

Now I’ve been told there are less errors with string interpolation. How can I translate that? I get hung up on the internal bit here:

@h[("s_fieldname_" + i.to_s)

I’m sure it’s out there. I appreciate any input.

Cheers, Bee

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

​Less errors? Well, it does magic stringification, if that's what you mean.
It also does less work (I'm pretty sure each
String#+ call
creates a new string object, which doesn't happen with interpolation.)

Here's the version of your line using interpolation for the outer string:

@f += "\t<label for=\"
#{
@h["s_fieldname_" + i.to_s]
}
\">
#{
@fieldname.capitalize
}
:"

And then, b
ecause
​ the stuff inside the interpolated block is Ruby code, you can interpolate
in there too, if you really want:

@f += "\t<label for=\"
#{
@h["s_fieldname_
#{i}
"]
​}
\">
#{
@fieldname.capitalize
}
:"

If you're not entirely comfortable with the nested interpolation, there's
nothing wrong with temporary variables:

    h = @h["s_fieldname_#{i}"]

@f += "\t<label for=\"
#{
h
}
\">
#{
@fieldname.capitalize
}
:"

And then, if you want fewer backslashes throughout, you can use the
alternative syntax for string literals[1]:

@f +=
%(
\t<label for="
#{
@h["s_fieldname_
#{i}
"]
}
">
#{
@fieldname.capitalize
}
:
)
    # OR
    h = @h["s_fieldname_#{i}"]

@f +=
%(
\t<label for="
#{
h
}
">
#{
@fieldname.capitalize
}
:"
​)

​... but that's strictly optional. Also consider `
@f << ...
` if you prefer to modify the string object in-place rather than creating a
new one.

There are many ways to achieve what you want; it comes down to personal
taste, requirements, and maintainability.

Cheers

[1]:
https://ruby-doc.org/core-2.3.0/doc/syntax/literals_rdoc.html#label-Percent+Strings

···

On 3 September 2016 at 00:08, Bee.Lists <bee.lists@gmail.com> wrote:

Hi folks. I come from a language where we don’t have string
interpolation. Here’s what I would usually write:

​​
@f += "\t<label for=\"" + @h[("s_fieldname_" + i.to_s)].to_s + "\">" +
@fieldname.capitalize + “:"

Now I’ve been told there are less errors with string interpolation. How
can I translate that? I get hung up on the internal bit here:

@h[("s_fieldname_" + i.to_s)

I’m sure it’s out there. I appreciate any input.

Cheers, Bee

--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

That’s working out fine.

Less errors in that if there is something missing, it still gets placed as a string with things missing, as opposed to an ISE.

Thanks to all the replies.

···

On Sep 2, 2016, at 6:13 PM, Matthew Kerwin <matthew@kerwin.net.au> wrote:

Here's the version of your line using interpolation for the outer string:

    @f += "\t<label for=\"#{@h["s_fieldname_" + i.to_s]}\">#{@fieldname.capitalize}:"

And then, because​ the stuff inside the interpolated block is Ruby code, you can interpolate in there too, if you really want:

    @f += "\t<label for=\"#{@h["s_fieldname_#{i}"]​}\">#{@fieldname.capitalize}:"

Cheers, Bee