Forum for beginners

S

Simon Blanco

Hi there, I was looking around for some beginners forum for ruby to
share little portions of code and help each other with some mistakes on
them, or trying to improve it.

I've been playing around with ruby for the last 2 weeks and I've
followed lots of guides and video tutorial, but when I tried to port
some C++ codes to ruby just to practise a bit I found lots of problems
with simple things as getting input data from the keyb. Finally I did:

print "Type the first number: "
STDOUT.flush
a = gets.to_i

print "Type the second number: "
STDOUT.flush
b = gets.to_i


But it doesn't look too good to me, is there any more elegant way to get
data? :D
Anyway... I'm not sure if this is the right place for asking such
things, so that's why I was asking for another specific beginners forum.

Thanks in advance!
 
R

Robert Dober

Hi there, I was looking around for some beginners forum for ruby to
share little portions of code and help each other with some mistakes on
them, or trying to improve it.

I've been playing around with ruby for the last 2 weeks and I've
followed lots of guides and video tutorial, but when I tried to port
some C++ codes to ruby just to practise a bit I found lots of problems
with simple things as getting input data from the keyb. Finally I did:

print "Type the first number: "
STDOUT.flush
a = gets.to_i

print "Type the second number: "
STDOUT.flush
b = gets.to_i


But it doesn't look too good to me, is there any more elegant way to get
data? :D
Anyway... I'm not sure if this is the right place for asking such
things, so that's why I was asking for another specific beginners forum.
Welcome
As there is no beginners forum, this is indeed the right place to ask,
I also daresay that you will be well taken care of :)
Now concerning your code above, it is a reasonable approach, maybe you
could do some things a little bit differently

print "Type the fourtysecond number:"
$stdout.flush # if necessary and some of us prefer the global variable
to the constant, but I forgot why, LOL
b = Integer gets.strip # This variation to using String#to_i might
be interesting, I show you in irb why

irb(main):003:0> Integer "42"
=> 42
irb(main):004:0> "42".to_i
=> 42
irb(main):005:0> puts "So far so good, but ;)"
So far so good, but ;)
=> nil
irb(main):006:0> "ade".to_i
=> 0
irb(main):007:0> Integer "ade"
ArgumentError: invalid value for Integer: "ade"
from (irb):7:in `Integer'
from (irb):7
from :0
irb(main):008:0> puts "Is maybe a better behaviour, is it not?"
Is maybe a better behaviour, is it not?

HTH
Robert
 
S

Simon Blanco

Ok, I changed the code a bit to try your answer:

print "First number: "
a = Integer gets.strip

print "Second number: "
b = gets.to_i

and I could totally get what you said ( I think :D ). The .to_i method
accepts and string while "Integer gets.strip" restricts the input data
to an integer and by the way strips any blank space, throwing an
ArgumentError if the input is uncompatible.

The other issue I had with STDOUT.flush was more related with Scite
console than with code itself. There's no need to use it in this little
program, at least. But I kinda got lost with this:

" > $stdout.flush # if necessary and some of us prefer the global
variable
to the constant, but I forgot why, LOL"

You mean creating a global variable at the beginning of the program, or
am I missing something?
 
R

Robert Dober

You mean creating a global variable at the beginning of the program, or
am I missing something?

No as STDOUT exists as built in, so das $stdout, same for STDERR,
STDIN, $stderr and $stdin.
The advantage of using $stdout is that you can change it at the
beginning of your program which might e.g. maeking some testing
easier.
But it is perfectly alright to use STDOUT :)
Cheers
Robert
 
S

Simon Blanco

No as STDOUT exists as built in, so das $stdout, same for STDERR,
STDIN, $stderr and $stdin.
The advantage of using $stdout is that you can change it at the
beginning of your program which might e.g. maeking some testing
easier.

That was revealing :D
I'll keep an eye on this when I have to solve input or keyb issues
 

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

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,085
Latest member
cryptooseoagencies

Latest Threads

Top