# Learning Ruby

**URL:** <https://www.ruby-forum.com/t/learning-ruby/74574>\
**Category:** Ruby\
**Created:** [November 7, 2006, 10:48am UTC](https://www.ruby-forum.com/t/learning-ruby/74574 "2006-11-07T10:48:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Urzahil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/258eb7/32.png) [@Urzahil](https://www.ruby-forum.com/u/Urzahil)\
**Post date:** [November 7, 2006, 10:48am UTC](https://www.ruby-forum.com/t/learning-ruby/74574/1 "2006-11-07T10:48:42Z")

</div>

Hi, I come from a little bit of C programming, an I’ve just started  
learning Ruby. So forgive me for the stupid question.  
Following the user guide, I found a regular expression example  
(rexg.rb), that doesn’t work for me.  
If I try to find an exact pattern everything is fine: for example,  
searching “foo” in “foobar” returns the correct result. But searching a  
regexp pattern doesn’t: “fo+” doesn’t find anything in “foobar”.  
I’m running Ruby 1.8 under Linux.  
Thank you in advance for your help,

Cristiano

---

<div class="post-metadata">

**Author:** ![Urzahil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/258eb7/32.png) [@Urzahil](https://www.ruby-forum.com/u/Urzahil)\
**Post date:** [November 7, 2006, 10:48am UTC](https://www.ruby-forum.com/t/learning-ruby/74574/2 "2006-11-07T10:48:43Z")

</div>

Urzahil wrote:

> Hi, I come from a little bit of C programming, an I’ve just started  
> learning Ruby. So forgive me for the stupid question.  
> Following the user guide, I found a regular expression example  
> (rexg.rb), that doesn’t work for me.  
> If I try to find an exact pattern everything is fine: for example,  
> searching “foo” in “foobar” returns the correct result. But searching a  
> regexp pattern doesn’t: “fo+” doesn’t find anything in “foobar”.  
> I’m running Ruby 1.8 under Linux.  
> Thank you in advance for your help,

Could you provide the examples you have tried? I did not really get you,  
as for me:

irb(main):003:0\> “foobar” =~ /foo/  
=\> 0  
irb(main):004:0\> “foobar” =~ /fo+/  
=\> 0

your example works. Please send the code snippets which did not work for  
you.

Peter  
[http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![Urzahil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/258eb7/32.png) [@Urzahil](https://www.ruby-forum.com/u/Urzahil)\
**Post date:** [November 7, 2006, 10:48am UTC](https://www.ruby-forum.com/t/learning-ruby/74574/3 "2006-11-07T10:48:45Z")

</div>

Cristiano M. wrote:

> > your example works. Please send the code snippets which did not work for  
> > print "str\> "  
> > STDOUT.flush  
> > str = gets  
> > break if not str  
> > str.chop!  
> > print "pat\> "  
> > STDOUT.flush  
> > re = gets  
> > break if not re  
> > re.chop!  
> > str.gsub! re, “#{st}\&#{en}”

Normal: this code is doing simple string lookup, as in this line, re  
is a String and not a Regexp. You should try this instead:

str.gsub!(Regexp.new(re), “#{st}\&#{en}”)

```
Vince
```

---

<div class="post-metadata">

**Author:** ![Urzahil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/258eb7/32.png) [@Urzahil](https://www.ruby-forum.com/u/Urzahil)\
**Post date:** [November 7, 2006, 10:48am UTC](https://www.ruby-forum.com/t/learning-ruby/74574/4 "2006-11-07T10:48:44Z")

</div>

Hi,

On Tue, 2006-10-31 at 20:00 +0900, Peter S. wrote:

> Could you provide the examples you have tried? I did not really get you,  
> as for me:
> 
> irb(main):003:0\> “foobar” =~ /foo/  
> =\> 0  
> irb(main):004:0\> “foobar” =~ /fo+/  
> =\> 0  
> your example works. Please send the code snippets which did not work for  
> you.

I did not explain me too well.  
This code works for me too. I was referring to the User Guide example.  
Here is the code that doesn’t work for me:

st = “\033[7m”  
en = “\033[m”

while TRUE  
print "str\> "  
STDOUT.flush  
str = gets  
break if not str  
str.chop!  
print "pat\> "  
STDOUT.flush  
re = gets  
break if not re  
re.chop!  
str.gsub! re, “#{st}\&#{en}”  
print str, “\n”  
end  
print “\n”

Thanks again,  
Cristiano

---

<div class="post-metadata">

**Author:** ![Urzahil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/258eb7/32.png) [@Urzahil](https://www.ruby-forum.com/u/Urzahil)\
**Post date:** [November 7, 2006, 10:48am UTC](https://www.ruby-forum.com/t/learning-ruby/74574/5 "2006-11-07T10:48:52Z")

</div>

On 10/31/06, Cristiano M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > =\> 0  
> > while TRUE  
> > str.gsub! re, “#{st}\&#{en}”  
> > print str, “\n”  
> > end  
> > print “\n”
> 
> Thanks again,  
> Cristiano

Not directly answering your question, but since you are still learning  
ruby:

########## LOTSA CODE #########  
st = “\033[7m”  
en = “\033[m”

$stdout.sync = true

loop do  
print "string\> "  
string = gets.chomp  
print "pattern\> "  
pattern = gets.chomp

puts string.gsub pattern, “#{st}\&#{en}”  
end  
######## LOTSA CODE END ########

i think that would be a more ‘rubylike’ way to write it… also making  
your intention a bit clearer 🙂  
you are not in C anymore, you can loosen your grip a little 😉

^manveru

---

<div class="post-metadata">

**Author:** ![Urzahil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/258eb7/32.png) [@Urzahil](https://www.ruby-forum.com/u/Urzahil)\
**Post date:** [November 7, 2006, 10:52am UTC](https://www.ruby-forum.com/t/learning-ruby/74574/6 "2006-11-07T10:52:54Z")

</div>

----- Original Message -----  
From: “Peter S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Tuesday, October 31, 2006 6:00 AM  
Subject: Re: Learning R.

> 

the 0 indicates that a match was found at offset 0

“foobar” =~ /oo/  
=\> 1

“foobar” =~ /boo/  
=\> nil

In ruby, 0 or empty string is not “false”, only nil or false are false  
anything else is true
