# Newbie needs a pointer

**URL:** <https://www.ruby-forum.com/t/newbie-needs-a-pointer/90670>\
**Category:** Ruby\
**Created:** [March 30, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/newbie-needs-a-pointer/90670 "2007-03-30T21:00:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 30, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/newbie-needs-a-pointer/90670/1 "2007-03-30T21:00:40Z")

</div>

Hi all, just started looking at Ruby in earnest today and my logic has  
failed me already. Can  
someone provide some subtle hints are to where the following code needs  
attension. Its currently  
stuck in a continuous loop, and ‘q’ doesnt exit the loop.

#a continous loop until ‘q’ is press  
puts “Please enter your name?”  
name=gets  
while (name!=‘q’)  
puts “welcome, #{name}Enter your name again:”  
name=gets

end

–

* * *

Posted with NewsLeecher v3.9 Beta 1  
Web @ [NewsLeecher - The Complete Usenet Package](http://www.newsleecher.com/?usenet)

* * *

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 30, 2007, 9:08pm UTC](https://www.ruby-forum.com/t/newbie-needs-a-pointer/90670/2 "2007-03-30T21:08:41Z")

</div>

Offhand, I’d guess that the return value from gets also includes the  
newline char(s).  
Try:  
name = gets.chomp

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 30, 2007, 9:09pm UTC](https://www.ruby-forum.com/t/newbie-needs-a-pointer/90670/3 "2007-03-30T21:09:41Z")

</div>

On Mar 30, 12:00 pm, removed\_email\_address@domain.invalid (removed\_email\_address@domain.invalid) wrote:

> end

irb(main):024:0\> name=gets  
wha  
=\> “wha\n”

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 30, 2007, 9:11pm UTC](https://www.ruby-forum.com/t/newbie-needs-a-pointer/90670/4 "2007-03-30T21:11:42Z")

</div>

removed\_email\_address@domain.invalid wrote:

> 

puts “Please enter your name?”

String name=gets  
puts name.methods  
while ( name.chomp != “q”)  
puts “welcome, #{name}Enter your name again:”  
name=gets  
end

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 30, 2007, 9:12pm UTC](https://www.ruby-forum.com/t/newbie-needs-a-pointer/90670/5 "2007-03-30T21:12:42Z")

</div>

removed\_email\_address@domain.invalid wrote:

> end

C:\WINDOWS\system32\>irb  
irb(main):001:0\> name = gets  
q  
=\> “q\n”  
irb(main):002:0\> name != ‘q’  
=\> true  
irb(main):003:0\> quit

C:\WINDOWS\system32\>ruby -v  
ruby 1.8.5 (2006-12-25 patchlevel 12) [i386-mswin32]

Try name = gets.chomp!

–  
Phillip “CynicalRyan” Gawlowski  
[http://cynicalryan.110mb.com/](http://cynicalryan.110mb.com/)

Eek! That was supposed to be My Special Law, _MY_ special law, I tell  
you!

T/

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 30, 2007, 9:57pm UTC](https://www.ruby-forum.com/t/newbie-needs-a-pointer/90670/6 "2007-03-30T21:57:50Z")

</div>

removed\_email\_address@domain.invalid wrote:

> #a continous loop until ‘q’ is press  
> puts “Please enter your name?”  
> name=gets  
> while (name!=‘q’)  
> puts “welcome, #{name}Enter your name again:”  
> name=gets
> 
> end

After name=gets name contains the string that the user typed including  
the  
enter. So if the user enters ‘q’, the value of name is “q\n”. You can  
remove  
the \n with the chomp method (i.e. use name=gets.chomp instead of  
name=gets).

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 30, 2007, 9:16pm UTC](https://www.ruby-forum.com/t/newbie-needs-a-pointer/90670/7 "2007-03-30T21:16:43Z")

</div>

your string to quit has garbage (like a \n or somefin) at the end.  
A little function like this may help

def same(somestring)  
input=gets  
if somestring==input  
puts “Same”  
else  
puts “#{input}!=#{somestring}”  
end  
end

That will show what you’re already seeing. So add a .length…  
def same(somestring)  
input=gets  
if somestring==input  
puts “Same”  
else  
puts “#{input}!=#{somestring}”  
puts “input.length=#{input.length}”  
puts “somestring.length=#{somestring.length}”  
end  
end

Humm…  
so what do we do? strip.  
you _could_ do

input=input.strip

But that’s not fun. Use a bang at the end of common functions to use  
the dangerous destructive fun version 🙂 usually a ! changes the  
object itself

so input.strip!

def same(somestring)  
input=gets  
input.strip!  
if somestring==input  
puts “Same”  
else  
puts “#{input}!=#{somestring}”  
puts “input.length=#{input.length}”  
puts “somestring.length=#{somestring.length}”  
end  
end

Does that help?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 31, 2007, 1:26am UTC](https://www.ruby-forum.com/t/newbie-needs-a-pointer/90670/8 "2007-03-31T01:26:18Z")

</div>

Thanks for all suggestions. IRB provides such a neat method to step  
through the program, would not  
have thought of it except for the suggestions.

When I decided to take a look at Ruby I was concerned that I would not  
be able to receive suppport  
(as python/php/asp.net still seem to reign supreme) , but this newsgroup  
has allayed all my  
doubts.

Thanks for the help.

btw =\> .chomp! works a .treat!

–

* * *

Posted with NewsLeecher v3.9 Beta 1  
Web @ [NewsLeecher - The Complete Usenet Package](http://www.newsleecher.com/?usenet)

* * *
