[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Replacing values

Ashikali Ashikali

2/19/2009 1:41:00 PM

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 .
--
Posted via http://www.ruby-....

15 Answers

Andrew Timberlake

2/19/2009 2:09:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Thu, Feb 19, 2009 at 3:41 PM, Ashikali Ashikali <ashikali.m@gmail.com>wrote:

> 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 .
> --
> Posted via http://www.ruby-....
>

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://ramblingso...
http://www.linkedin.com/in/andrew...

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

Ashikali Ashikali

2/19/2009 3:00:00 PM

0

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-....
>>
>
> 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://ramblingso...
> http://www.linkedin.com/in/andrew...
>
> "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

Robert Klemme

2/19/2009 3:09:00 PM

0

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 .

>> 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://ramblingso...
>> http://www.linkedin.com/in/andrew...
>>
>> "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 ) .

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


--
remember.guy do |as, often| as.you_can - without end

lasitha

2/19/2009 3:17:00 PM

0

On Thu, Feb 19, 2009 at 7:39 PM, Andrew Timberlake
<andrew@andrewtimberlake.com> wrote:
> On Thu, Feb 19, 2009 at 3:41 PM, Ashikali Ashikali <ashikali.m@gmail.com>wrote:
>
>> 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 .
>> --
>>
>
> 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.

lasitha

2/19/2009 3:33:00 PM

0

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.

Robert Klemme

2/19/2009 3:41:00 PM

0

2009/2/19 lasitha <lasitha.ranatunga@gmail.com>:
> 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).

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

--
remember.guy do |as, often| as.you_can - without end

lasitha

2/19/2009 5:41:00 PM

0

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.

Robert Klemme

2/20/2009 6:48:00 AM

0

On 19.02.2009 18:40, lasitha wrote:
> 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.

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

lasitha

2/20/2009 8:33:00 AM

0

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.

sheldonlg

3/19/2013 7:13:00 PM

0

On 3/19/2013 2:42 PM, Yisroel Markov wrote:
> On Tue, 19 Mar 2013 14:05:17 +0000 (UTC), Shelly
> <sheldonlg@thevillages.net> said:
>
>> On 3/19/2013 9:51 AM, Yisroel Markov wrote:
>>> On Mon, 18 Mar 2013 20:23:27 +0000 (UTC), DoD <danskisanjar@gmail.com>
>>> said:
>>>
>>>> On Mar 18, 2:50 pm, Shelly <sheldo...@thevillages.net> wrote:
>>>
>>> [snip]
>>>
>>>>> No, you misunderstood. Jew use teffillin as an aide in prayer. It is
>>>>> part of the ritual of praying at certain times. Likewise, their symbols
>>>>> are aides in prayer. They are not prayer TO those pieces of wood or
>>>>> marble, nor do they believe that those pieces of wood or marble ARE
>>>>> god[like]. They are concentration aides, not the objects of prayer --
>>>>> same as teffillin.
>>>>
>>>> Again it is exactly right. Prayer aides. One thing that really annoys
>>>> Catholics is when Protestants/Evangelicals try to tell us that we pray
>>>> to statues and that we worship Mary. You never catch Catholics
>>>> talking bad about Prot/Evan customs.
>>>
>>> For the record, David, I agree with what you and Shelly say here.
>>> That's not what upset me.
>>
>> Well, this is what I meant (to which you now agree)
>
> Always had.
>
>> and if something
>> else upset you, I am sorry, but I don't know what. I never meant to be
>> "insulting", and I *DO* see teffillin as part of prayer ritual,
>
> Yes. Although one may wear them at other times, and doing so is
> considered praiseworthy.
>
>> hence "prayer/concentration aides".
>
> No. Doesn't follow. (BTW, an "aide" is a person.)

Ok, Ok, already. "prayer/concentration aids".

>
>> If you consider that insulting, then I
>> don't know why, but that is how I view them.
>
> And you are wrong. I tell you that as a habitual tefillin-wearer. I'm
> sure there are Jews who think of tefillin as "prayer/concentration
> aids", what with the New Age influences out there, or nothing but
> "part of the ritual of praying at certain times," but IMHO that's not
> "kosher." (Of course, the main thing is to wear tefillin, rather than
> to have correct ideas about them.)
>
> What was insulting is how cavalierly you drew not only a functional,
> but a substantial analogy between tefillin and those non-Jewish
> symbols. Even if not idolatrous (which is halakhically doubtful), the
> described use of those symbols is big-time anathema to Jews. I
> understand that you didn't mean to be insulting, of course.

Well, we'll leave it at that.

--
Shelly