# Continuous input until semicolon reached

**URL:** <https://www.ruby-forum.com/t/continuous-input-until-semicolon-reached/117027>\
**Category:** Ruby\
**Created:** [October 12, 2007, 6:23am UTC](https://www.ruby-forum.com/t/continuous-input-until-semicolon-reached/117027 "2007-10-12T06:23:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [October 12, 2007, 6:23am UTC](https://www.ruby-forum.com/t/continuous-input-until-semicolon-reached/117027/1 "2007-10-12T06:23:43Z")

</div>

Hello

I am doing a program in which I want to allow my user to keep keying in  
until they put semicolon then the prompt will process the data. It is  
something like mysql way. for example

mysql\> select

> - from  
> user;

I want to have something like this.

the other question, how can I read the last char as char in a string  
because it keeps returning the ASCII value

e.g.

str = “text here;”  
len = str.len  
semicolon = str[len - 1]  
this gives me the ascii value.

Regards

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [October 12, 2007, 7:13am UTC](https://www.ruby-forum.com/t/continuous-input-until-semicolon-reached/117027/2 "2007-10-12T07:13:32Z")

</div>

Shuaib Z. wrote:

> Hello
> 
> I am doing a program in which I want to allow my user to keep keying in  
> until they put semicolon then the prompt will process the data. It is  
> something like mysql way. for example
> 
> mysql\> select
> 
> > - from  
> > user;
> 
> I want to have something like this.

while gets.chomp != “;”  
#code here

> the other question, how can I read the last char as char in a string  
> because it keeps returning the ASCII value

Sorry but i dont quite understand your 2nd question ☹ Maybe elaborate  
a little?

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [October 12, 2007, 7:45am UTC](https://www.ruby-forum.com/t/continuous-input-until-semicolon-reached/117027/3 "2007-10-12T07:45:35Z")

</div>

On Oct 12, 12:13 am, Michael L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I want to have something like this.
> 
> while gets.chomp != “;”  
> #code here

I don’t think this does what you think it does. String#chomp takes a  
character as its argument, with ‘\n’ being the default. The final  
character of the String is removed iff that character is the same as  
the argument character. String#chomp then returns whatever remains of  
the calling String, not the removed character.

Instead, try

all\_input = “”  
while line = gets  
line.chomp!  
next if line.nil?  
all\_input \<\< line  
break if line[-1].chr == ‘;’  
end  
puts “got: #{all\_input}”

> > the other question, how can I read the last char as char in a string  
> > because it keeps returning the ASCII value
> 
> Sorry but i dont quite understand your 2nd question ☹ Maybe elaborate  
> a little?

I think the second question refers to the fact that  
“hello”[0] =\> 104

when  
“hello”[0].chr =\> “h”  
is the desired behavior.

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [October 12, 2007, 7:47am UTC](https://www.ruby-forum.com/t/continuous-input-until-semicolon-reached/117027/4 "2007-10-12T07:47:20Z")

</div>

On Oct 12, 12:41 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > - from  
> > > > the argument character. String#chomp then returns whatever remains of  
> > > > end  
> > > > puts “got: #{all\_input}”

which isn’t completely correct either, but the general idea is there  
and it’s my bedtime.

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [October 12, 2007, 10:57am UTC](https://www.ruby-forum.com/t/continuous-input-until-semicolon-reached/117027/5 "2007-10-12T10:57:32Z")

</div>

Shuaib Z. wrote:

> Hello
> 
> I am doing a program in which I want to allow my user to keep keying in  
> until they put semicolon then the prompt will process the data.

gets(";")  
or  
gets(";\n") if you want the semicolon to always be at the end of the  
line.

Beware that if you use gets(";") and the user enters “foo;bar” the “bar”  
will  
be stuck in the buffer until you next read from stdin.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [October 18, 2007, 4:15am UTC](https://www.ruby-forum.com/t/continuous-input-until-semicolon-reached/117027/6 "2007-10-18T04:15:39Z")

</div>

removed\_email\_address@domain.invalid wrote:

> On Oct 12, 12:13 am, Michael L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Instead, try
> 
> all\_input = “”  
> while line = gets  
> line.chomp!  
> next if line.nil?  
> all\_input \<\< line  
> break if line[-1].chr == ‘;’  
> end  
> puts “got: #{all\_input}”

I used this code and it works but I have a question and I need to  
confirm my answer.

next if line.nil?

next here is equivalent to continue in C. Am I right, i looked in ruby  
library, I did not find continue.

Regards

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [October 18, 2007, 10:02am UTC](https://www.ruby-forum.com/t/continuous-input-until-semicolon-reached/117027/7 "2007-10-18T10:02:41Z")

</div>

Shuaib Z. wrote:

> the other question, how can I read the last char as char in a string  
> because it keeps returning the ASCII value
> 
> e.g.
> 
> str = “text here;”  
> len = str.len  
> semicolon = str[len - 1]  
> this gives me the ascii value.

str = “text here;”  
semicolon = str[-1, 1]

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [October 18, 2007, 8:13am UTC](https://www.ruby-forum.com/t/continuous-input-until-semicolon-reached/117027/8 "2007-10-18T08:13:14Z")

</div>

On 18/10/2007, Shuaib Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> next if line.nil?
> 
> next here is equivalent to continue in C. Am I right, i looked in ruby  
> library, I did not find continue.

Yes.

– Thomas A.
