Forum: Ruby Replacing values

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
054515cb92a507d2b549b8fe4c281fff?d=identicon&s=25 Ashik Ali (ashikalim)
on 2009-02-19 14:40
Analyse this code

a = 10
b = "This is #{a}" # prints "This is 10"
b = 'This is #{c}' # prints "This is \#{c}"
c = 18

b # "This is \#{c}"

Here I want ,
"This is 18"

Question ,
How can I convert '' value to "" value .
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-02-19 15:09
(Received via mailing list)
On Thu, Feb 19, 2009 at 3:41 PM, Ashikali Ashikali
<ashikali.m@gmail.com>wrote:

> "This is 18"
>
> Question ,
> How can I convert '' value to "" value .
> --
> Posted via http://www.ruby-forum.com/.
>

Your question is not clear

Changing a ' to a " is as simple as typing a different character.
In your example you are typing a string literal so typing it differently
should get you the results you're looking for.

If you're working on another string, you can do: <string>.gsub(/'/, '"')

Andrew Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
054515cb92a507d2b549b8fe4c281fff?d=identicon&s=25 Ashik Ali (ashikalim)
on 2009-02-19 16:00
Andrew Timberlake wrote:
> On Thu, Feb 19, 2009 at 3:41 PM, Ashikali Ashikali
> <ashikali.m@gmail.com>wrote:
>
>> "This is 18"
>>
>> Question ,
>> How can I convert '' value to "" value .
>> --
>> Posted via http://www.ruby-forum.com/.
>>
>
> Your question is not clear
>
> Changing a ' to a " is as simple as typing a different character.
> In your example you are typing a string literal so typing it differently
> should get you the results you're looking for.
>
> If you're working on another string, you can do: <string>.gsub(/'/, '"')
>
> Andrew Timberlake
> http://ramblingsonrails.com
> http://www.linkedin.com/in/andrewtimberlake
>
> "I have never let my schooling interfere with my education" - Mark Twain

my question is not that converting ' to " . Apart from this ,
when we declare string as "" , our ruby replace the variable value into
actual
value like this ,
c = 10
b = "This is #{c}"
puts b
#it prints "This is 10"
In above, it start to execute and replace c variable value so it prints
,
"This is 10" .

m = 'This is #{d}'  #single quote
In above , it does not replace d variable( not yet created ) .

d = 15
#if I do puts m
'This is #{d}'

What I want is when we converting ' to " it start to replace the value d
.
is there any method available to do this .
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-02-19 16:09
(Received via mailing list)
2009/2/19 Ashikali Ashikali <ashikali.m@gmail.com>:
> Andrew Timberlake wrote:
>> On Thu, Feb 19, 2009 at 3:41 PM, Ashikali Ashikali
>> <ashikali.m@gmail.com>wrote:
>>
>>> "This is 18"
>>>
>>> Question ,
>>> How can I convert '' value to "" value .

>> http://www.linkedin.com/in/andrewtimberlake
> #it prints "This is 10"
> In above, it start to execute and replace c variable value so it prints
> ,
> "This is 10" .
>
> m = 'This is #{d}'  #single quote
> In above , it does not replace d variable( not yet created ) .

No, it does not do the replacement because you used a single quoted
string.

> d = 15
> #if I do puts m
> 'This is #{d}'
>
> What I want is when we converting ' to " it start to replace the value d
> .
> is there any method available to do this .

To do what?  Are you thinking of something like this?

irb(main):001:0> a = 'This is #{b}'
=> "This is \#{b}"
irb(main):002:0> b = 10
=> 10
irb(main):003:0> eval("\"#{a}\"")
=> "This is 10"


robert
E16e84e861c1815ce11ba7bd851c857d?d=identicon&s=25 lasitha (Guest)
on 2009-02-19 16:19
(Received via mailing list)
On Thu, Feb 19, 2009 at 7:39 PM, Andrew Timberlake
<andrew@andrewtimberlake.com> wrote:
>>
>> Here I want ,
>> "This is 18"
>>
>> Question ,
>> How can I convert '' value to "" value .
>> --
>>
>
> Your question is not clear

If i might venture a guess, i think Ashikali wants the interpolation
to occur lazily.
I don't know a way to construct a string that defers interpolation
(though i suspect there must be one).

If not, you could use 'eval' or perhaps a Proc:

$: irb
01> s = lambda {|x| "This is #{x}" }
--> #<Proc:0x735a64@(irb):1 (lambda)>
02> s.call 1
--> "This is 1"
03> s.call 2
--> "This is 2"

HTH.
lasitha.
E16e84e861c1815ce11ba7bd851c857d?d=identicon&s=25 lasitha (Guest)
on 2009-02-19 16:33
(Received via mailing list)
On Thu, Feb 19, 2009 at 8:47 PM, lasitha <lasitha.ranatunga@gmail.com>
wrote:
> I don't know a way to construct a string that defers interpolation
> (though i suspect there must be one).
>
> If not, you could use 'eval' or perhaps a Proc:
>
Oops, sorry - please ignore the bit about using a Proc.  It doesn't
solve anything since all it does is parameterize the result and you
could have done that with just a regular method.

So at this point eval is all i can think of (see Robert's post).
lasitha.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-02-19 16:41
(Received via mailing list)
2009/2/19 lasitha <lasitha.ranatunga@gmail.com>:
> So at this point eval is all i can think of (see Robert's post).
Still I would consider an ordinary method or a lambda superior to
using eval.  I try to avoid eval whenever possible because of security
and other implications.  For me it's really only a last resort for
things that can't be done otherwise (e.g. defining methods which must
have an explicit block parameter).

Kind regards

robert
E16e84e861c1815ce11ba7bd851c857d?d=identicon&s=25 lasitha (Guest)
on 2009-02-19 18:41
(Received via mailing list)
On Thu, Feb 19, 2009 at 9:10 PM, Robert Klemme
<shortcutter@googlemail.com> wrote:
> 2009/2/19 lasitha <lasitha.ranatunga@gmail.com>:
>> Oops, sorry - please ignore the bit about using a Proc.  It doesn't
>> solve anything since all it does is parameterize the result and you
>> could have done that with just a regular method.
>>
>> So at this point eval is all i can think of (see Robert's post).
>
> Still I would consider an ordinary method or a lambda superior to
> using eval.  I try to avoid eval whenever possible  [...]

True, thanks for prodding me to think it through again :)
The following is indeed nicer than eval:

$: irb
01> x = 1
--> 1
02> s = lambda { "This is #{x}" }
--> #<Proc:0x733930@(irb):2 (lambda)>
03> s.call
--> "This is 1"
04> x = 2
--> 2
05> s.call
--> "This is 2"

Ashikali, the caveat here is that 'x' has to be assigned before the
Proc is created.  The initial assignment could be to nil though, the
interpreter just needs it to have seen it.

Cheers,
lasitha.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-02-20 07:50
(Received via mailing list)
On 19.02.2009 18:40, lasitha wrote:
>
> 04> x = 2
> --> 2
> 05> s.call
> --> "This is 2"
>
> Ashikali, the caveat here is that 'x' has to be assigned before the
> Proc is created.  The initial assignment could be to nil though, the
> interpreter just needs it to have seen it.

I would prefer an explicit block parameter though - or a method for that
matter.  It's more encapsulated, i.e. you have a clear interface and do
not rely on the rather unobvious closure.

Cheers

  robert
E16e84e861c1815ce11ba7bd851c857d?d=identicon&s=25 lasitha (Guest)
on 2009-02-20 09:33
(Received via mailing list)
On Fri, Feb 20, 2009 at 12:19 PM, Robert Klemme
<shortcutter@googlemail.com> wrote:
> I would prefer an explicit block parameter though - or a method for that
> matter.  It's more encapsulated, i.e. you have a clear interface and do not
> rely on the rather unobvious closure.
>

Certainly, if that is an option for the OP.  I only suggested the
closure based solution as an alternative to eval - both solutions are
inferior to a parameterized approach.

Cheers,
lasitha.
This topic is locked and can not be replied to.