Forum for beginners

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? :smiley:
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!

···

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

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? :smiley:
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 :slight_smile:
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 :wink:
=> 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
irb(main):008:0> puts "Is maybe a better behaviour, is it not?"
Is maybe a better behaviour, is it not?

HTH
Robert

···

On Thu, Jun 12, 2008 at 11:57 AM, Simon Blanco <cowkiller123@gmail.com> wrote:
  from :0
--
http://ruby-smalltalk.blogspot.com/

---
As simple as possible, but not simpler.
Albert Einstein

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 :smiley: ). 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?

···

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

<snip>

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 :slight_smile:
Cheers
Robert

···

On Thu, Jun 12, 2008 at 4:33 PM, Simon Blanco <cowkiller123@gmail.com> wrote:

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

--
http://ruby-smalltalk.blogspot.com/

---
As simple as possible, but not simpler.
Albert Einstein

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 :smiley:
I'll keep an eye on this when I have to solve input or keyb issues

···

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