# Forum for beginners

**URL:** <https://www.ruby-forum.com/t/forum-for-beginners/140262>\
**Category:** Ruby\
**Created:** [June 12, 2008, 11:58am UTC](https://www.ruby-forum.com/t/forum-for-beginners/140262 "2008-06-12T11:58:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![saikun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@saikun](https://www.ruby-forum.com/u/saikun)\
**Post date:** [June 12, 2008, 11:58am UTC](https://www.ruby-forum.com/t/forum-for-beginners/140262/1 "2008-06-12T11:58:04Z")

</div>

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? 😃  
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!

---

<div class="post-metadata">

**Author:** ![saikun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@saikun](https://www.ruby-forum.com/u/saikun)\
**Post date:** [June 12, 2008, 1:08pm UTC](https://www.ruby-forum.com/t/forum-for-beginners/140262/2 "2008-06-12T13:08:12Z")

</div>

On Thu, Jun 12, 2008 at 11:57 AM, Simon B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> print "Type the first number: "  
> 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

[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

As simple as possible, but not simpler.  
Albert Einstein

---

<div class="post-metadata">

**Author:** ![saikun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@saikun](https://www.ruby-forum.com/u/saikun)\
**Post date:** [June 12, 2008, 4:34pm UTC](https://www.ruby-forum.com/t/forum-for-beginners/140262/3 "2008-06-12T16:34:20Z")

</div>

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 😃 ). 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?

---

<div class="post-metadata">

**Author:** ![saikun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@saikun](https://www.ruby-forum.com/u/saikun)\
**Post date:** [June 12, 2008, 7:05pm UTC](https://www.ruby-forum.com/t/forum-for-beginners/140262/4 "2008-06-12T19:05:33Z")

</div>

On Thu, Jun 12, 2008 at 4:33 PM, Simon B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

As simple as possible, but not simpler.  
Albert Einstein

---

<div class="post-metadata">

**Author:** ![saikun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@saikun](https://www.ruby-forum.com/u/saikun)\
**Post date:** [June 13, 2008, 11:18am UTC](https://www.ruby-forum.com/t/forum-for-beginners/140262/5 "2008-06-13T11:18:53Z")

</div>

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