Is it ever necessary to use 'chomp' before using `to_i` or `to_f`?
There is no need to use chomp
method because:
String#chomp
returns a new String with the given record separator removed from the end of str (if present). If $/
has not been changed from the default Ruby record separator, then chomp
also removes carriage return characters (that is it will remove "\n", "\r", and "\r\n"). Here are some examples.
String#to_f
returns the result of interpreting leading characters in str
as a floating point number. Extraneous characters past the end of a valid number are ignored. If there is not a valid number at the start of str
, 0.0 is returned. This method never raises an exception. Here are some examples for to_f
.
Whats wrong with the modulus operator in this Ruby code?
Your result will always be false
because gets.chomp
returns a String:
number = gets.to_i
The above code will work but you must always type an integer. There are better ways to manage checking to see if the input is valid.
So you could check first like this:
number = gets
factors_to_three(number.to_i) if number.is_a?(Integer)
How to return a number using to_i when the input starts with $
Meditate on these:
"$1\n"[1..-1].to_i # => 1
"$1.99\n"[1..-1].to_i # => 1
"$100.99\n"[1..-1].to_i # => 100
This breaks down when there's a leading -
:
"-$100.99\n"[1..-1].to_i # => 0
That can be fixed using sub
instead of a slice:
"-$100.99\n".sub('$', '').to_i # => -100
The problem is, money is not all integers, so to_i
is probably not really what you want. Instead you should be using to_f
:
"$1\n".sub('$', '').to_f # => 1.0
"$1.99\n".sub('$', '').to_f # => 1.99
"$100.99\n".sub('$', '').to_f # => 100.99
"-$100.99\n".sub('$', '').to_f # => -100.99
"$-100.99\n".sub('$', '').to_f # => -100.99
Note: It isn't necessary to use chomp
to remove the trailing new-line. to_i
and to_f
will stop when they see a non-digit:
"1\n".to_i # => 1
"1.99\n".to_f # => 1.99
"1 1\n".to_i # => 1
"1.99 2\n".to_f # => 1.99
Again, because of this behavior, "\n"
and 1\n
or 2\n
will be ignored.
Ruby random number generator
number
is a number, choice
is a string. You need to parse the string for this to work:
choice = gets.chomp.to_i
How to return a number using to_i when the input starts with $
Meditate on these:
"$1\n"[1..-1].to_i # => 1
"$1.99\n"[1..-1].to_i # => 1
"$100.99\n"[1..-1].to_i # => 100
This breaks down when there's a leading -
:
"-$100.99\n"[1..-1].to_i # => 0
That can be fixed using sub
instead of a slice:
"-$100.99\n".sub('$', '').to_i # => -100
The problem is, money is not all integers, so to_i
is probably not really what you want. Instead you should be using to_f
:
"$1\n".sub('$', '').to_f # => 1.0
"$1.99\n".sub('$', '').to_f # => 1.99
"$100.99\n".sub('$', '').to_f # => 100.99
"-$100.99\n".sub('$', '').to_f # => -100.99
"$-100.99\n".sub('$', '').to_f # => -100.99
Note: It isn't necessary to use chomp
to remove the trailing new-line. to_i
and to_f
will stop when they see a non-digit:
"1\n".to_i # => 1
"1.99\n".to_f # => 1.99
"1 1\n".to_i # => 1
"1.99 2\n".to_f # => 1.99
Again, because of this behavior, "\n"
and 1\n
or 2\n
will be ignored.
Check if a number entered by the user with gets.chomp is a float in ruby
puts "What are the first number you want to divide"
number1 = gets.chomp.to_i
=> 100
puts "What is the second number?"
number2 = gets.chomp.to_i
=> 3
# Regular math
result_a = number1 / number2
puts "#{number1} / #{number2} = #{result_a}"
=> 100 / 3 = 33 # Integer class persists...
# Use a ruby library instead! Numeric#divmod
result_b = number1.divmod(number2)
puts "Result: #{result_b}"
=> [33, 1] # [quotient, modulus]
How can I divide two strings in Ruby with gets.chomp?
The command gets
stands for "get a string". You are trying to divide a string by a number.
Change the line
lbs = gets.chomp
to
lbs = gets.chomp.to_i
to convert the string to an integer, or use to_f
if you prefer using floats.
How can I divide two strings in Ruby with gets.chomp?
The command gets
stands for "get a string". You are trying to divide a string by a number.
Change the line
lbs = gets.chomp
to
lbs = gets.chomp.to_i
to convert the string to an integer, or use to_f
if you prefer using floats.
Related Topics
How to Convert Boolean Values to Integers
Slicing Params Hash for Specific Values
Cocoon Add Association, How to Limit Number of Associations
Why Does a Rails App on Heroku Serve Assets via All.CSS and Locally via Individual Files
How Might I Simulate a Private Browsing Experience in Watir? (Selenium)
Why Is This Not a Syntax Error
Rails 3 Cli Executes Commands Really Slow
Call Ruby Script from Powershell
Ruby If VS End of the Line If Behave Differently
Ruby Inside JavaScript Block [Slim Template]
"Msvcrt-Ruby18.Dll Was Not Found" with Ruby
Remove Subdomain from String in Ruby
How to Set a Variable from a Helper Method for Inclusion in a SASS SCSS Stylesheet
Optional Parens in Ruby for Method with Uppercase Start Letter
How to More Elegantly Remove Duplicate Items Across All Elements of a Ruby Array