Forum: Ruby User Input Integers

73a86b3136bfdda99045ba5de86dd9ec?d=identicon&s=25 Sayth Renshaw (flebber)
on 2010-12-22 03:42
(Received via mailing list)
using 1.9.1

I have google searched this but there seems to be a very broad array
of answers none very consistent.

Can someone put to bed the exact way to get user input with "Integers"
and specify how many decimals to output too in this case 2 decimals.

Here is a basic program(not working) to calculate netpay.

puts "How many hours did you work?"
hours = gets.chomp()._to.i
puts "What is your tax rate?"
taxrate = gets.chomp()._to.i
tax = (100 - taxrate)
netpay = ((hours * 12.00) *  tax))
puts "Your netpay is, $" $netpay
09a32175057418748822c587ac08c429?d=identicon&s=25 Abinoam Jr. (abinoampraxedes_m)
on 2010-12-22 04:00
(Received via mailing list)
On Tue, Dec 21, 2010 at 11:41 PM, flebber <flebber.crue@gmail.com>
wrote:
> using 1.9.1

> I have google searched this but there seems to be a very broad array
> of answers none very consistent.

:-) :-D Sorry... but I really don't believe it!

> Can someone put to bed the exact way to get user input with "Integers"
> and specify how many decimals to output too in this case 2 decimals.

Yes! To do your home work? Ok :-)

> Here is a basic program(not working) to calculate netpay.

> puts "How many hours did you work?"
> hours = gets.chomp()._to.i
> puts "What is your tax rate?"
> taxrate = gets.chomp()._to.i
> tax = (100 - taxrate)
> netpay = ((hours * 12.00) * tax))
> puts "Your netpay is, $" $netpay

Use this code bellow solely as a guide.

puts "How many hours did you work?"
hours = gets.chomp().to_i
puts "What is your tax rate?"
taxrate = gets.chomp().to_i
tax = (100 - taxrate)
netpay = ((hours * 12.00) *  tax)
printf "Your netpay is, US$ %.2f", netpay

Aside the code errors, I think your calcs are wrong.

Wouldn't it be something like...

netpay = ((hours * 12.00) *  tax) / 100 # with this 100 dividing it.

Abinoam Jr.
73a86b3136bfdda99045ba5de86dd9ec?d=identicon&s=25 Sayth Renshaw (flebber)
on 2010-12-22 10:55
(Received via mailing list)
On Dec 22, 1:59pm, "Abinoam Jr." <abin...@gmail.com> wrote:
> Yes! To do your home work? Ok :-)
> Use this code bellow solely as a guide.
>
> Wouldn't it be something like...
>
> netpay = ((hours * 12.00) * tax) / 100 # with this 100 dividing it.
>
> Abinoam Jr.

The Top 3 results from google for Ruby user input integers

> > I have google searched this but there seems to be a very broad array
> > of answers none very consistent.
>
> :-) :-D Sorry... but I really don't believe it!

*top result stackoverflow

Answer
s = []
for i in 1..3
    puts "What is point " + i.to_s + " ?"    # asks for input
    s.push gets.split(" ").map {|x| x.to_i }
end
if s[1][0] - s[0][0] == 0 # notice the '=='.
    puts 'It worked!'
end

*In second place well sadly the result was stackoverflow again.

For multiple integer input

q = 1
$stdin.readline.split.each {|n| q *= n.to_i }
p q


*result number 3 is drum roll me with this thread
http://www.ruby-forum.com/topic/709485

Sadly no decent Ruby Documentation comes up in the google search, even
sader this thread is already in the top 3.
73a86b3136bfdda99045ba5de86dd9ec?d=identicon&s=25 Sayth Renshaw (flebber)
on 2010-12-22 10:57
(Received via mailing list)
On Dec 22, 1:59pm, "Abinoam Jr." <abin...@gmail.com> wrote:
> Yes! To do your home work? Ok :-)
> Use this code bellow solely as a guide.
>
> Wouldn't it be something like...
>
> netpay = ((hours * 12.00) * tax) / 100 # with this 100 dividing it.
>
> Abinoam Jr.

Oh and flattery will get you nowhere.

> Yes! To do your home work? Ok :-)

My oldest girl is starting school in 2011 and no my name isn't Kerney.
09a32175057418748822c587ac08c429?d=identicon&s=25 Abinoam Jr. (abinoampraxedes_m)
on 2010-12-22 14:00
(Received via mailing list)
> Oh and flattery will get you nowhere.

The only thing I can say now is (sincerely) "sorry"!
Ed2f1dbdac5bc8aa5ef8e0052d2f530a?d=identicon&s=25 Jose Sonhy (shimazaky1980)
on 2014-03-17 09:48
Hi Guys;

I am new to Ruby.

I have a problem getting user input with gets method.

The following is a very simple code snippet for me to try this method:

print "What's your name? "
my_name = gets
puts "Nice to meet you " + my_name + "!"

When I try to run the code, this is the error that i get:
What's your name? TypeError: can't convert nil into String

How do i fix it? By the way, I am using Ruby 2.0. I have tried on my PC
as well as on my android phone with Ruboto, still the error message. All
the books I have been reading, they have been using exactly this format:
my_name = gets

Is there a special way of doing it?
A7fca2db6ff3f4583df98043bd5166f9?d=identicon&s=25 Panagiotis Atmatzidis (Guest)
on 2014-03-17 13:24
(Received via mailing list)
Attachment: signature.asc (832 Bytes)
Hello,

On 17 Μαρ 2014, at 09:48 , Jose Sonhy <lists@ruby-forum.com> wrote:

> Hi Guys;
>
> I am new to Ruby.

Welcome to wonderland :-)

> What's your name? TypeError: can't convert nil into String
How are you executing this? Because here works fine:

┌─[atma@air] - [~] - [Δευ Μαρ 17, 01:22]
└─[$] <> cat test.rb
print "What's your name? "
my_name = gets
puts "Nice to meet you " + my_name + "!"
┌─[atma@air] - [~] - [Δευ Μαρ 17, 01:22]
└─[$] <> ruby test.rb
What's your name? Nick
Nice to meet you Nick
!
┌─[atma@air] - [~] - [Δευ Μαρ 17, 01:22]
└─[$] <> ruby --version
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin13.0.0]
┌─[atma@air] - [~] - [Δευ Μαρ 17, 01:22]
└─[$] <>


>
> How do i fix it? By the way, I am using Ruby 2.0. I have tried on my PC
> as well as on my android phone with Ruboto, still the error message. All
> the books I have been reading, they have been using exactly this format:
> my_name = gets
>
> Is there a special way of doing it?

Other than adding '.chomp' to escape \$ not really :-)

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



Panagiotis (atmosx) Atmatzidis

email:  atma@convalesco.org
URL:  http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5

"The fool doth think he is wise, but the wise man knows himself to be a
fool." - William Shakespeare
Ed2f1dbdac5bc8aa5ef8e0052d2f530a?d=identicon&s=25 Jose Sonhy (shimazaky1980)
on 2014-03-20 14:01
@ Panagiotis;

I appreciate your help.

My Name I Sony.

On irb it works fine. However it is when i execute it on the command
prompt from the source code.

Please somebody help, i really want to learn this language, but if i am
not able to interact with it, is pointless.
403ceafdd0ceb79b5eaa352af7c68da6?d=identicon&s=25 Danielle H. (danielle_h)
on 2014-06-18 20:39
Jose Sonhy wrote in post #1140461:
> @ Panagiotis;
>
> I appreciate your help.
>
> My Name I Sony.
>
> On irb it works fine. However it is when i execute it on the command
> prompt from the source code.
>
> Please somebody help, i really want to learn this language, but if i am
> not able to interact with it, is pointless.

I am just beginning as well. When I run it on my computer it worked
fine. You may check that you are running a current version of Ruby.

- Open command prompt
- type ruby -v

If this is the problem go here
https://www.ruby-lang.org/en/installation/

Some other things I have found helpful are http://www.codecademy.com/
and Learn to Program, 2nd Edition by Chris Pine

The way I understand to do your example is like the following:

print "What's your name?"
name = gets.chomp

puts "Nice to meet you #{name}!"


There are probably better ways, but much like you I am still very new to
this.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.