Replacing values

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 .

On Thu, Feb 19, 2009 at 3:41 PM, Ashikali A.
[email protected]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: .gsub(/'/, ‘"’)

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

“I have never let my schooling interfere with my education” - Mark Twain

Andrew T. wrote:

On Thu, Feb 19, 2009 at 3:41 PM, Ashikali A.
[email protected]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: .gsub(/'/, ‘"’)

Andrew T.
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 .

2009/2/19 Ashikali A. [email protected]:

Andrew T. wrote:

On Thu, Feb 19, 2009 at 3:41 PM, Ashikali A.
[email protected]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

On Thu, Feb 19, 2009 at 8:47 PM, lasitha [email protected]
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.

On Thu, Feb 19, 2009 at 7:39 PM, Andrew T.
[email protected] 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.

On Thu, Feb 19, 2009 at 9:10 PM, Robert K.
[email protected] wrote:

2009/2/19 lasitha [email protected]:

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 :slight_smile:
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.

2009/2/19 lasitha [email protected]:

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

On Fri, Feb 20, 2009 at 12:19 PM, Robert K.
[email protected] 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.

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