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.
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
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
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?
On Oct 12, 12:13 am, Michael L. [email protected] 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.
On Oct 12, 12:41 am, “[email protected]” [email protected] 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.
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
[email protected] wrote:
On Oct 12, 12:13 am, Michael L. [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
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]
On 18/10/2007, Shuaib Z. [email protected] 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.