How do I input a variable floating point number into Ruby Programs

I want to evaluate the following interactively:

   28977682.9/x

It is the Weins’s Displacement Law Equation

How do I write a Ruby P. to evaluate it

There is a equivalent program on Wolfram Alpha

How do I write a Ruby P. to evaluate it

You just did. :slight_smile:

I want to input the variable, which changes, from the console and
evaluate multiple examples without entering the constant multiple times.

On 2013-06-23, at 5:03 PM, Mike S. [email protected] wrote:

#!/usr/bin/env ruby

require ‘rubygems’
require ‘highline/import’

CONST = 28977682.9

begin
loop do
num = ask("Enter number: ", Float)
puts CONST * num

Oops, that * should be a /.

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

On 2013-06-23, at 2:51 PM, “Michael P F.” [email protected] wrote:

I want to input the variable, which changes, from the console and
evaluate multiple examples without entering the constant multiple times.


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

Do yo mean something like this (which uses the highline gem to prompt
for input and coerce it to the right type):

#!/usr/bin/env ruby

require ‘rubygems’
require ‘highline/import’

CONST = 28977682.9

begin
loop do
num = ask("Enter number: ", Float)
puts CONST * num
end
rescue EOFError
puts “Bye!”
end

END

Hope this helps,

Mike

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

I get the following error message in Ubuntu Terminal

michael@michael-desktop:~/Desktop$ ruby<weins.txt
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in require': cannot load such file -- highline/import (LoadError) from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:inrequire’
from -:4:in `’
michael@michael-desktop:~/Desktop$ ^C
michael@michael-desktop:~/Desktop$

Thanks for the effort, anyway

Mike Finerty

It runs but doesn’t pause for a input. Here is the Terminal output

michael@michael-desktop:~/Desktop$ ruby<weins.txt
Enter number: enter a number please.
Enter number: Bye!
michael@michael-desktop:~/Desktop$

             Mike

On 2013-06-23, at 5:28 PM, “Michael P F.” [email protected] wrote:

Thanks for the effort, anyway

Mike Finerty


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

That means you don’t have the highline gem installed, it is a little
more verbose if you don’t use that, we have to check for the input being
exhausted and for input which can’t be turned into a Float so maybe:

#!/usr/bin/env ruby

CONST = 28977682.9

catch (:done) do
loop do
print "Enter number: "
throw :done unless response = gets

if num = Float(response) rescue nil
  puts CONST / num
else
  puts "enter a number please."
end

end
end

puts “Bye!”

END

Hope this helps,

Mike

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

IT WORKS! Thank you. I will give you credit.

                   Mike

On 2013-06-23, at 6:04 PM, “Michael P F.” [email protected] wrote:

It runs but doesn’t pause for a input. Here is the Terminal output

michael@michael-desktop:~/Desktop$ ruby<weins.txt
Enter number: enter a number please.
Enter number: Bye!
michael@michael-desktop:~/Desktop$

            Mike

If you saved the program in weins.txt then try

ruby wiens.txt

When you run you run ruby <weins.txt then the contents of weins.txt are
used as the standard input stream which ruby consumes and executes, then
hits the end of file.

On my laptop:

ratdog:tmp mike$ cat try.rb
#!/usr/bin/env ruby

CONST = 28977682.9

catch (:done) do
loop do
print "Enter number: "
throw :done unless response = gets

if num = Float(response) rescue nil
  puts CONST / num
else
  puts "enter a number please."
end

end
end

puts “Bye!”

END
ratdog:tmp mike$ ruby try.rb
Enter number: 1
28977682.9
Enter number: 2
14488841.45
Enter number: 2.3
12598992.565217393
Enter number: banana
enter a number please.
Enter number: Bye!

(I hit Ctrl-D at the last prompt)

Hope this helps,

Mike

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.