puts "I will make a random #{"word"} in brackets" #I don't get the point of
that.
In this particular case, I could have easily just coded:
puts "I will make a random word in brackets"
Is there any advantage to doing the former? I'm not actually defining a
variable. It's just useful as a placeholder in the future when I'm ready
to put a variable in?
puts "I will make a random #{"word"} in brackets" #I don't get the point of
that.
In this particular case, I could have easily just coded:
puts "I will make a random word in brackets"
Exactly.
Is there any advantage to doing the former?
Not in this use case. But keep in mind, the point of #{...} is that an
arbitrary expression can go there, will be evaluated when the
surrounding expression is executed and whatever the result it will be
transformed to a String via #to_s. That use case is much broader than
just inserting a constant string.
puts "I will make #{2 + rand(17)} random words in brackets"
I'm not actually defining a
variable. It's just useful as a placeholder in the future when I'm ready to
put a variable in?
But why do that? You can start with the String constant and later add
the #{...} with the arbitrary expression that you need there.
Cheers
robert
···
On Fri, Sep 15, 2017 at 6:08 PM, Bill Bisco <billbisco@gmail.com> wrote:
Good point Robert; thank you, that would probably be a little better.
···
On Fri, Sep 15, 2017 at 12:38 PM, Robert Klemme <shortcutter@googlemail.com> wrote:
On Fri, Sep 15, 2017 at 6:08 PM, Bill Bisco <billbisco@gmail.com> wrote:
> I have become aware that this line of code works
>
> puts "I will make a random #{"word"} in brackets" #I don't get the point
of
> that.
>
> In this particular case, I could have easily just coded:
>
> puts "I will make a random word in brackets"
Exactly.
> Is there any advantage to doing the former?
Not in this use case. But keep in mind, the point of #{...} is that an
arbitrary expression can go there, will be evaluated when the
surrounding expression is executed and whatever the result it will be
transformed to a String via #to_s. That use case is much broader than
just inserting a constant string.
puts "I will make #{2 + rand(17)} random words in brackets"
> I'm not actually defining a
> variable. It's just useful as a placeholder in the future when I'm
ready to
> put a variable in?
But why do that? You can start with the String constant and later add
the #{...} with the arbitrary expression that you need there.