Forum: Ruby Code review for beginner

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0c70a862d79a5c426b72b93edcd37308?d=identicon&s=25 Joseph We (animatedjw)
on 2017-04-10 01:05
Hello All,

I'm new to coding with Ruby. I would like to know if there is a better
way or proper way, to write the below code below. Pretty much this code
will get any 2 numbers from user input and divide them while rejecting
any none integers and the number 0. Any help is appreciated. I also read
somewhere that using too many exceptions are bad?

def add(name, n)
  puts name.to_f / n
end

    puts "please provide first number"
    begin
    name = Integer(gets.chomp)
    rescue ArgumentError
    puts "Please input a valid number!"
    retry
 end
    while name == 0
    puts "please provide a number greater than 0"
     begin
    name = Integer(gets.chomp)
    rescue ArgumentError
    puts "Please input a valid number!"
    retry
end
end
    puts "input second number"
    begin
    n = Integer(gets.chomp)
    rescue ArgumentError
    puts "Please input a valid number!"
    retry
end
    while n == 0
    puts "please provide a number greater than 0"
    begin
    n = Integer(gets.chomp)
    rescue ArgumentError
    puts "Please input a valid number!"
    retry
end
end
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-04-10 14:01
I somehow fail to see, where you are checking that the number which was
entered is positive.

You ask for suggestions to write the program in a better way. This means
we don't talk about correctness, but of style. This in turn means that
the program is already correct in the sense that it provides correct
output. Did you verfiy that it is correct?

Ronald
0c70a862d79a5c426b72b93edcd37308?d=identicon&s=25 Joseph We (animatedjw)
on 2017-04-10 14:15
Ronald Fischer wrote in post #1185789:
> I somehow fail to see, where you are checking that the number which was
> entered is positive.
>
> You ask for suggestions to write the program in a better way. This means
> we don't talk about correctness, but of style. This in turn means that
> the program is already correct in the sense that it provides correct
> output. Did you verfiy that it is correct?
>
> Ronald

correct sorry for the confusion. This code already produces the correct
input. I guess I was just looking to see if there was somehow another
way to shorten it.
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2017-04-10 14:28
> I would like to know if there is a better way or proper way, to write the below
> code below.

A lot can be written better in ruby.

I think your code is semi-ok for a first try.

What I would do is to get rid of the repetition. Since you repeat
the same action, you should capture that part and re-use it.

> I also read somewhere that using too many exceptions are bad?

It is a way of control flow but you should ask whether you would
need it in your current setup.

I do not think that you would need it in your current setup
because you are in full control over the input and can sanitize
it.

I would recommend to use a class too - while a class has a
slight additional layer (class Foo; end) around the code,
the long-time benefit is really really helping you design
SIMPLER code in the long run.

Example:

---


class GrabNumbers

  alias e puts

  def initialize
    run
  end

  def add(name, n)
    puts name.to_f / n
  end

  def obtain_user_input
    $stdin.gets.chomp
  end

  def obtain_valid_number(input = obtain_user_input)
    input.to_i
  end; alias to_valid_number obtain_valid_number

  def please_provide_a_number_greater_than_zero
    e 'Please provide a number greater than 0.'
  end

  def run
    e 'Please provide the first number.'
    loop {
      @number = obtain_user_input
      case @number
      when 'q','exit','break'
        break
      end
      _ = to_valid_number(@number)
      case _
      when 0
        please_provide_a_number_greater_than_zero
        @number = obtain_valid_number
      else
        if _ > 0
          e 'The number is `'+_.to_s+'`.'
          break
        else # Must be lower than 0.
          please_provide_a_number_greater_than_zero
        end
      end
    }
  end

end

GrabNumbers.new


---

Don't assume that the above is great code... I just threw
it together in about 5 minutes or so. It kinda works too
though, so it's not awful. You of course can still use
Integer() in your own code - I just did not think that
it was necessary.

If you have not yet written a class in ruby, try it -
the concept is quite simple and once you understood
it, ONCE, you can apply it for the rest of your life
to all of ruby, just about.

There are also some simpler ways to write the above
by the way, I just have an awkward style in ruby -
feel free to find a style that sits well for you.

I should also add that, once it is a class, the part
that you want to change lateron, becomes really super
simple.

Also always try to write minimal test cases for your
class there, so that you can run these tests lateron
when you make some changes. For a simple class like
this it would be overkill, but for more complex use
cases, minimal tests are good.
0c70a862d79a5c426b72b93edcd37308?d=identicon&s=25 Joseph We (animatedjw)
on 2017-04-10 15:04
Thank you all for the help! This helps me a lot. I am still learning on
classes and will use this as an example to study.
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-04-10 15:35
Joseph We wrote in post #1185790:
> This code already produces the correct
> input.


Are you sure?

If you enter 0 for the first number, and on the second question ("please
provide a number greater than 0") you enter -1, is this input really
rejected, as it should?
0c70a862d79a5c426b72b93edcd37308?d=identicon&s=25 Joseph We (animatedjw)
on 2017-04-10 17:05
I meant to change around the actual wording on that part. -1 can be
accepted but only 0 and letters should not be allowed.
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-04-10 17:21
Joseph We wrote in post #1185795:
> I meant to change around the actual wording on that part. -1 can be
> accepted but only 0 and letters should not be allowed.

I see.

In this case, the first improvement for your code would be a bit of
refactoring (aside from providing nicer formatting):

You have a lot of blocks which go like this:

puts SOME PROMPT
begin
    name = Integer(gets.chomp)
rescue ArgumentError
    puts "Please input a valid number!"
    retry
end
.... Do something with name ....

Instead of repeating the same over and over, put it into a method
(functin) having SOME PROMPT as paramer.

Try to do this, then post your improved program, and then we will
continue further .....
0c70a862d79a5c426b72b93edcd37308?d=identicon&s=25 Joseph We (animatedjw)
on 2017-04-10 18:12
Thanks for the assistance. I'll get started on this today and post an
update.
Fac860976a7910f13fcd3b08d1c887a1?d=identicon&s=25 Sam Sandy (sadesh)
on 2017-04-13 09:59
FuGenX is working on Ruby On Rails and have a 8+ years of experiences
and strong portfolio of client work done.

Reach :

http://fugenx.com/services/mobile-application-development/

http://fugenx.com/services/iphone-application-development/

http://fugenx.com/services/android-application-development/
B6966d88ac4a246ea0379a35bb0e2dec?d=identicon&s=25 Max Pittman (maxpittman)
on 2017-04-18 14:07
That's really good resource for appdev. Probably could be useful for our
website. By the way, if you need qualified help with college writing @
https://essayshark.com/buy-cheap-essays.html  do not hesitate to
contact.
This topic is locked and can not be replied to.