Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > simple gsub method

Reply
Thread Tools

simple gsub method

 
 
Jack Smith
Guest
Posts: n/a
 
      10-01-2008
I have simplified my method for searching test to the following:

def replacer(a, b)
x = "now is the time for all good men to come to the aid of their
country"
x.gsub(/#{a}/, b)
puts x
end

puts "enter what to replace: "
j = gets
puts "enter replacer word: "
h = gets

replacer(j, h)

Can someone help me determine why gsub can't match my "a" value?

thanks
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      10-01-2008
Alle Wednesday 01 October 2008, Jack Smith ha scritto:
> I have simplified my method for searching test to the following:
>
> def replacer(a, b)
> x = "now is the time for all good men to come to the aid of their
> country"
> x.gsub(/#{a}/, b)
> puts x
> end
>
> puts "enter what to replace: "
> j = gets
> puts "enter replacer word: "
> h = gets
>
> replacer(j, h)
>
> Can someone help me determine why gsub can't match my "a" value?
>
> thanks


Strings returned by gets end in a newline, which doesn't exist in your string,
so no match happens. To solve this, you can do

j = gets.chomp

and the same for h.

I hope this helps

Stefano


 
Reply With Quote
 
 
 
 
Jack Smith
Guest
Posts: n/a
 
      10-01-2008
It does help Stefano as I had forgotton about \n so thanks for that but
my script still does not match on my "a" value even with the chomp
method added.




Stefano Crocco wrote:
> Alle Wednesday 01 October 2008, Jack Smith ha scritto:
>> j = gets
>> puts "enter replacer word: "
>> h = gets
>>
>> replacer(j, h)
>>
>> Can someone help me determine why gsub can't match my "a" value?
>>
>> thanks

>
> Strings returned by gets end in a newline, which doesn't exist in your
> string,
> so no match happens. To solve this, you can do
>
> j = gets.chomp
>
> and the same for h.
>
> I hope this helps
>
> Stefano


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      10-01-2008
Jack Smith wrote:
> x.gsub(/#{a}/, b)


You don't use the return value of gsub, so it is thrown away and this line
basically does nothing. Calling gsub on x, does not change x (gsub! would).

HTH,
Sebastian
--
Jabber: http://www.velocityreviews.com/forums/(E-Mail Removed)
ICQ: 205544826

 
Reply With Quote
 
Jack Smith
Guest
Posts: n/a
 
      10-01-2008
I take that back Stefano....you solved it for me!
I forgot to add the x.gsub! ....when I did, my script is working.

thanks a REAL lot for helping me with this!

j


Jack Smith wrote:
> It does help Stefano as I had forgotton about \n so thanks for that but
> my script still does not match on my "a" value even with the chomp
> method added.
>
>
>
>
> Stefano Crocco wrote:
>> Alle Wednesday 01 October 2008, Jack Smith ha scritto:
>>> j = gets
>>> puts "enter replacer word: "
>>> h = gets
>>>
>>> replacer(j, h)
>>>
>>> Can someone help me determine why gsub can't match my "a" value?
>>>
>>> thanks

>>
>> Strings returned by gets end in a newline, which doesn't exist in your
>> string,
>> so no match happens. To solve this, you can do
>>
>> j = gets.chomp
>>
>> and the same for h.
>>
>> I hope this helps
>>
>> Stefano


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
yet another private method `gsub' called for nil:NilClass error Mr. Bill Ruby 2 12-30-2010 02:24 PM
Creating a gsub! method for Arrays Derek Cannon Ruby 7 03-26-2010 08:34 PM
simple gsub question \' \` what? Dustin Anderson Ruby 3 03-30-2007 10:31 PM
gsub method question Hoppy Ruby 4 05-10-2006 06:17 PM
gsub and gsub! are inconsistent aurelianito Ruby 9 11-09-2005 01:38 PM



Advertisments