-1

For some reason this code always multiplies b (2.54) by unit_number, regardless of whether I put enter "cm" or "in":

puts "Welcome to my unit conversion calculator. This calculator can convert
between Centimeters and Inches. If you could like to convert Centimeters to Inches, write: in. If you would like to convert Inches to centimeters, write: cm."

unit = gets.to_s

puts " how many of your unit would you like to convert"
unit_number = gets.to_f

a = 0.39370079
b = 2.54

if unit == 'in'      
  puts "your conversion is: #{ (unit_number * a)}"    
else unit == 'cm'
  puts "your conversion is: #{ (unit_number * b)}"
end
Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
user2759592
  • 261
  • 1
  • 2
  • 10
  • Welcome to Stack Overflow. Please improve your question by posting some [properly formatted](http://stackoverflow.com/editing-help) code you've applied to the problem, all **relevant** error messages exactly as they appear, and whatever samples you're testing against. Also, please include a properly-formatted sample of your **expected output** so folks understand the results you're trying to achieve. – Todd A. Jacobs Sep 08 '13 at 23:29
  • Hi sorry but I am confused about this question as well, are you looking for a solution like this. [read input from console](http://stackoverflow.com/questions/6556280/read-input-from-console-in-ruby) – User128848244 Sep 08 '13 at 23:31
  • Presumably, `gets` will equal either `cm` or `in`. If this is true, then `gets.to_f` will __always__ equal `0.0`. Am I missing something? – zeantsoi Sep 09 '13 at 00:03
  • You don't need to type `gets.to_s`. `gets` is short for get string, which means that you're going to be getting a string anyways. – David Sep 09 '13 at 00:48

3 Answers3

3

gets will capture the stdin input including the trailing new line (enter), so unit won't be 'cm' and instead will be "cm\n". You will need to remove the trailing "\n" using chomp:

unit = gets.chomp

You don't need to call to_s to the return value of gets because it is already a String, and the else statement should be elsif unit == 'cm' or else if unit == 'cm'.

akhanubis
  • 4,202
  • 1
  • 27
  • 19
  • thank you very much. for some reason the programs runs fine when using just: else and doesn't run when using: elseif. Ill just leave it how it is. thanks again – user2759592 Sep 09 '13 at 00:15
  • [`strip`](http://ruby-doc.org/core-2.0.0/String.html#method-i-strip) may be better than `chomp`. – Andrew Marshall Sep 09 '13 at 00:47
  • `chomp` is best in this situations are it more clearly indicates that you are trying to remove trailing whitespace. More importantly, `chomp` automatically handles both Unix-style `\n` line endings and Windows-style `\r\n` line endings (which would require explicit separate handing via `strip`). – Jim Stewart Sep 09 '13 at 01:04
1

Your conditional invokes an else statement, when what you actually need is an elsif statement:

elsif unit == 'cm'

Basically, what you currently have is a situation where the first of your conditions (i.e., if unit == 'in') is not being met (see Pablo B's answer for reasons why). However, the second condition is met because it's an else statement. Since the first condition is always false, the else condition will always be true.

If, instead, you invoke an elsif statement, the conditional will first check whether unit == 'in'; if it's not, then it checks whether unit == 'cm'. If neither are true, then the condition will return nil.

Community
  • 1
  • 1
zeantsoi
  • 25,857
  • 7
  • 69
  • 61
  • To clarify, `else unit == 'cm'` is two statements. To see the logic flow, put the `else` on a line by itself. Then it will execute two statements: the first will be the expression `unit == 'cm'`, which will evaluate to `false` and simply be ignored, as it's not part of another expression. Then the `puts` will execute. You can use `else if` or the combination `elseif`. You still need to fix the trailing `\n` issue mentioned by @Pablo B. as well. – Jim Stewart Sep 09 '13 at 01:01
  • Very right. I understand that `else unit == 'cm'` is two statements. I'm suggesting that the poster has conflated `else unit == 'cm'` with `elseif unit == 'cm'`. – zeantsoi Sep 09 '13 at 01:05
  • @nonocut, what do you mean? – zeantsoi Sep 09 '13 at 03:16
  • You need to use `elsif` instead of `elseif` – saihgala Sep 09 '13 at 03:41
  • While that's true, an alternative syntax is `else if`, FWIW. – zeantsoi Sep 09 '13 at 03:47
0

I know exactly how do you feel. Debugging is a pain. @akhanubis already pointed you towards #chomp method needed to process your gets return value. While I assume that you wrote your code for didactic purposes, if you ever need to convert physical units for real, just install sy gem. After gem install sy, you can:

require 'sy/imperial'

7.5.inch.in :cm
#=> 19.05
42.cm.in :inch
#=> 16.5354
Boris Stitnicky
  • 12,444
  • 5
  • 57
  • 74