1
\$\begingroup\$

As a rookie to programming, i'm really excited about finishing my first program.

But I know I only learn by making mistakes and through constructive criticism.

I'm open to any feedback regarding my use of branching, and any way to "clean up" my code.

puts 'Hi! Pick a number between 0 and 10'
number = gets.chomp
puts 'Got it; your number is ' + number.to_s + ', right?'
check1 = gets.chomp

if
  check1 == 'yes'
  puts 'awesome. now double your number. What\'s your new number?'
else
  puts 'Oops. Either: A. I oopsie-daisied up or B. you\'re lying.'
end

number_doubled = gets.chomp
if
  number_doubled.to_i == number.to_i * 2
  puts 'cool. Now add 6. What\'s the new number?'
else
  puts 'Oopsie-daisies'
end

number_plus_6 = gets.chomp
if
  number_plus_6.to_i == number.to_i * 2 + 6
  puts 'cool; Now divide your new answer by 2; What\'s your new number?'
else
  puts 'Oopsie-daisies'
end

number_halved = gets.chomp
if
  number_halved.to_i == number_plus_6.to_i / 2
  puts 'cool. Now, if you subtract the number you originally picked, ' + number + ', you\'re answer will be 3. Is it?'
else
  puts 'Oopsie-daisies'
end

check2 = gets.chomp
if
  check2 == 'yes'
  puts 'See? I told you i was a smarty!!'
else
  puts 'hmm, i think you\'re bad at math'
end
\$\endgroup\$
2
  • 3
    \$\begingroup\$ I think it is a mistake of one sort or another to add expletives to the code. Seems a bit gratuitous. \$\endgroup\$ Commented Aug 25, 2017 at 8:55
  • \$\begingroup\$ Fair enough. But if the only criticism is my poor choice of gratuitous language, than I think you're talking more about your advanced skill in etiquette and less about the code. Which is a level of coding I hope to achieve, so thanks for looking! :) \$\endgroup\$ Commented Aug 28, 2017 at 4:42

1 Answer 1

1
\$\begingroup\$

I think in general this is fine. The only things I'd point out are stylistic really.

  1. In Ruby, many people would prefer double-quoted strings, which allow you to: Got it; your number is #{number}, right?. Note that this also relies on an implicit #to_s on number. It also allows single quotes to be used without escaping.
  2. Look out for variable naming. Naming things correctly is notoriously hard, even after years of experience, and number is a bit generic.
  3. I would definitely put the if conditional on the same line as the if. If the expression is too long or a bit difficult to instinctively understand, assign its value to a variable with a meaningful name (e.g. correctly_added_six = number_plus_6.to_i == number.to_i * 2 + 6) so your condition is more easily understood (e.g. if correctly_added_six)

Good start, though!

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for the positive feedback! I think I just learned about idea #3, so it makes a little more sense what you're saying. \$\endgroup\$ Commented Sep 2, 2017 at 1:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.