[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

continuous input until semicolon reached

Shuaib Zahda

10/12/2007 4:24:00 AM

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
--
Posted via http://www.ruby-....

7 Answers

Michael Linfield

10/12/2007 5:14:00 AM

0

Shuaib Zahda 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?
--
Posted via http://www.ruby-....

yermej

10/12/2007 5:41:00 AM

0

On Oct 12, 12:13 am, Michael Linfield <globyy3...@hotmail.com> wrote:
> Shuaib Zahda 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

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.

yermej

10/12/2007 5:44:00 AM

0

On Oct 12, 12:41 am, "yer...@gmail.com" <yer...@gmail.com> wrote:
> On Oct 12, 12:13 am, Michael Linfield <globyy3...@hotmail.com> wrote:
>
> > Shuaib Zahda 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
>
> 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.

> > > 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.


Sebastian Hungerecker

10/12/2007 8:57:00 AM

0

Shuaib Zahda 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
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Shuaib Zahda

10/18/2007 2:16:00 AM

0

yermej@gmail.com wrote:
> On Oct 12, 12:13 am, Michael Linfield <globyy3...@hotmail.com> 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

--
Posted via http://www.ruby-....

Thomas Adam

10/18/2007 6:12:00 AM

0

On 18/10/2007, Shuaib Zahda <shuaib.zahda@gmail.com> 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 Adam

7stud --

10/18/2007 8:03:00 AM

0

Shuaib Zahda 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]
--
Posted via http://www.ruby-....