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: :smiley:](https://emoji.discourse-cdn.com/twitter/smiley.png?v=12)
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: :slight_smile:](https://emoji.discourse-cdn.com/twitter/slight_smile.png?v=12)
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: :wink:](https://emoji.discourse-cdn.com/twitter/wink.png?v=12)
=> 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