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 Program to evaluate it

There is a equivalent program on Wolfram Alpha

···

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

How do I write a Ruby Program to evaluate it

You just did. :slight_smile:

···

On Sun, Jun 23, 2013 at 12:02 PM, Michael P F. <lists@ruby-forum.com> wrote:

I want to evaluate the following interactively:

       28977682.9/x

It is the Weins's Displacement Law Equation

How do I write a Ruby Program to evaluate it

There is a equivalent program on Wolfram Alpha

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

--
- Cliff Rosson

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/.

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:in `require'
  from -:4:in `<main>'
michael@michael-desktop:~/Desktop$ ^C
michael@michael-desktop:~/Desktop$

Thanks for the effort, anyway

   Mike Finerty

···

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

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

···

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

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

                       Mike

···

--
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

···

On 2013-06-23, at 2:51 PM, "Michael P F." <lists@ruby-forum.com> 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/\.

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

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

···

On 2013-06-23, at 5:28 PM, "Michael P F." <lists@ruby-forum.com> wrote:

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:in `require'
from -:4:in `<main>'
michael@michael-desktop:~/Desktop$ ^C
michael@michael-desktop:~/Desktop$

Thanks for the effort, anyway

  Mike Finerty

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

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

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

···

On 2013-06-23, at 6:04 PM, "Michael P F." <lists@ruby-forum.com> 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

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

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

Oops, that * should be a /.

···

On 2013-06-23, at 5:03 PM, Mike Stok <mike@stok.ca> wrote:

On 2013-06-23, at 2:51 PM, "Michael P F." <lists@ruby-forum.com> wrote:

end
rescue EOFError
puts "Bye!"
end

__END__

Hope this helps,

Mike

--

Mike Stok <mike@stok.ca>
Mike Stok

The "`Stok' disclaimers" apply.

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.