continuous input until semicolon reached

S

Shuaib Zahda

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
 
M

Michael Linfield

Shuaib said:
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

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?
 
Y

yermej

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}"
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.
 
Y

yermej

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}"

which isn't completely correct either, but the general idea is there
and it's my bedtime.
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.
 
S

Sebastian Hungerecker

Shuaib said:
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
 
S

Shuaib Zahda

On Oct 12, 12:13 am, Michael Linfield <[email protected]> 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
 
7

7stud --

Shuaib said:
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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top