# Code review for beginner

**URL:** <https://www.ruby-forum.com/t/code-review-for-beginner/245321>\
**Category:** Ruby\
**Created:** [April 10, 2017, 1:05am UTC](https://www.ruby-forum.com/t/code-review-for-beginner/245321 "2017-04-10T01:05:10Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [April 10, 2017, 1:05am UTC](https://www.ruby-forum.com/t/code-review-for-beginner/245321/1 "2017-04-10T01:05:10Z")

</div>

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?

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

```

---

<div class="post-metadata">

**Author:** ![joesniff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@joesniff](https://www.ruby-forum.com/u/joesniff)\
**Post date:** [April 10, 2017, 2:01pm UTC](https://www.ruby-forum.com/t/code-review-for-beginner/245321/2 "2017-04-10T14:01:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joesniff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@joesniff](https://www.ruby-forum.com/u/joesniff)\
**Post date:** [April 10, 2017, 2:15pm UTC](https://www.ruby-forum.com/t/code-review-for-beginner/245321/3 "2017-04-10T14:15:47Z")

</div>

Ronald F. 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.

---

<div class="post-metadata">

**Author:** ![joesniff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@joesniff](https://www.ruby-forum.com/u/joesniff)\
**Post date:** [April 10, 2017, 2:28pm UTC](https://www.ruby-forum.com/t/code-review-for-beginner/245321/4 "2017-04-10T14:28:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joesniff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@joesniff](https://www.ruby-forum.com/u/joesniff)\
**Post date:** [April 10, 2017, 3:04pm UTC](https://www.ruby-forum.com/t/code-review-for-beginner/245321/5 "2017-04-10T15:04:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![joesniff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@joesniff](https://www.ruby-forum.com/u/joesniff)\
**Post date:** [April 10, 2017, 3:35pm UTC](https://www.ruby-forum.com/t/code-review-for-beginner/245321/6 "2017-04-10T15:35:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![joesniff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@joesniff](https://www.ruby-forum.com/u/joesniff)\
**Post date:** [April 10, 2017, 5:21pm UTC](https://www.ruby-forum.com/t/code-review-for-beginner/245321/7 "2017-04-10T17:21:39Z")

</div>

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 …

---

<div class="post-metadata">

**Author:** ![joesniff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@joesniff](https://www.ruby-forum.com/u/joesniff)\
**Post date:** [April 10, 2017, 5:05pm UTC](https://www.ruby-forum.com/t/code-review-for-beginner/245321/8 "2017-04-10T17:05:54Z")

</div>

I meant to change around the actual wording on that part. -1 can be  
accepted but only 0 and letters should not be allowed.

---

<div class="post-metadata">

**Author:** ![joesniff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@joesniff](https://www.ruby-forum.com/u/joesniff)\
**Post date:** [April 10, 2017, 6:12pm UTC](https://www.ruby-forum.com/t/code-review-for-beginner/245321/9 "2017-04-10T18:12:17Z")

</div>

Thanks for the assistance. I’ll get started on this today and post an  
update.

---

<div class="post-metadata">

**Author:** ![joesniff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@joesniff](https://www.ruby-forum.com/u/joesniff)\
**Post date:** [April 13, 2017, 9:59am UTC](https://www.ruby-forum.com/t/code-review-for-beginner/245321/10 "2017-04-13T09:59:00Z")

</div>

FuGenX is working on Ruby On Rails and have a 8+ years of experiences  
and strong portfolio of client work done.

Reach :

> **[Mobile App Development Company In Bangalore, Mumbai, Hyderabad, India, USA](https://www.fugenx.com/services/mobile-application-development/)**
>
> FuGenX Top Mobile App development company in Bangalore, Delhi, Mumbai, India offering various best mobile application development services like iPhone & Android.

> **[Best iPhone App Development Company in Bangalore, Mumbai India and USA](https://www.fugenx.com/services/iphone-application-development/)**
>
> FuGenX is the best iPhone app development company in Bangalore Mumbai India. We stay top in premiere iPhone application development companies list.

> **[Best Android Application Development Company in Bangalore, India, USA](https://www.fugenx.com/services/android-application-development/)**
>
> Best Android Application Development Company, FuGenX offers Taylor made Android app development solutions to increase the business productivity with our extensive production and design process

---

<div class="post-metadata">

**Author:** ![joesniff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@joesniff](https://www.ruby-forum.com/u/joesniff)\
**Post date:** [April 18, 2017, 2:07pm UTC](https://www.ruby-forum.com/t/code-review-for-beginner/245321/11 "2017-04-18T14:07:31Z")

</div>

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](https://essayshark.com/buy-cheap-essays.html) do not hesitate to  
contact.

---

<div class="post-metadata">

**Author:** ![mike88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a587f6/32.png) [@mike88](https://www.ruby-forum.com/u/mike88)\
**Post date:** [June 20, 2019, 10:48am UTC](https://www.ruby-forum.com/t/code-review-for-beginner/245321/14 "2019-06-20T10:48:21Z")

</div>

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

Corrcted version  
[mspvip](https://mspfreevip.net/)

---

<div class="post-metadata">

**Author:** ![SouravGoswami](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/souravgoswami/32/2543_2.png) [@SouravGoswami](https://www.ruby-forum.com/u/SouravGoswami)\
**Post date:** [June 20, 2019, 5:31pm UTC](https://www.ruby-forum.com/t/code-review-for-beginner/245321/15 "2019-06-20T17:31:55Z")

</div>

If all you are trying to do is a simple division of two floating point numbers:

```auto
STDOUT.sync = true

begin
	puts (1..2).map { |i| print("Enter Number #{i}: ") || STDIN.gets.to_f }.then { |x| "#{x.join('/')} = #{x.reduce(:/).round(3)}" }

rescue Interrupt, SignalException
	puts
	exit! 0
end

```

Output:

```auto
⮚ ruby ccc.rb
Enter Number 1: 200
Enter Number 2: 100
200.0/100.0 = 2.0

⮚ ruby ccc.rb 
Enter Number 1: -111
Enter Number 2: -112
-111.0/-112.0 = 0.9910714285714286

⮚ ruby ccc.rb
Enter Number 1: 0
Enter Number 2: 100
0.0/100.0 = 0.0

⮚ ruby ccc.rb
Enter Number 1: 100
Enter Number 2: 0
100.0/0.0 = Infinity

⮚ ruby ccc.rb
Enter Number 1: 500_000.5
Enter Number 2: 100_000.1
500000.5/100000.1 = 5.0

```

---

<div class="post-metadata">

**Author:** ![lia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@lia](https://www.ruby-forum.com/u/lia)\
**Post date:** [March 16, 2021, 9:09am UTC](https://www.ruby-forum.com/t/code-review-for-beginner/245321/18 "2021-03-16T09:09:13Z")

</div>

This is helpful content. To get more rush into this blog with a proper way of writing you are suggested to take [paper writing help](https://penmypaper.com/). Will be waiting for more content like this from here.

---

<div class="post-metadata">

**Author:** ![turnermiya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnermiya](https://www.ruby-forum.com/u/turnermiya)\
**Post date:** [April 7, 2021, 6:38pm UTC](https://www.ruby-forum.com/t/code-review-for-beginner/245321/19 "2021-04-07T18:38:49Z")

</div>

coding has become an integral part of modern education. the way software applications are taking over our daily lives, it is imperative for the young generation to learn coding, however it can be quite challenging at time and can often demotivate novice candidates. That is why it is important to take help from expert professionals. If you are having any issue with your coding, take help from the code [writing service](https://eduhelphub.com/college-essay-writing-service) provides by leading IT engineers, who have work experience for a decade in the domain.

---

<div class="post-metadata">

**Author:** ![KrisCasper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/df705f/32.png) [@KrisCasper](https://www.ruby-forum.com/u/KrisCasper)\
**Post date:** [September 28, 2023, 8:51am UTC](https://www.ruby-forum.com/t/code-review-for-beginner/245321/20 "2023-09-28T08:51:03Z")

</div>

Hello, i am new here, can i ask a question?

---

<div class="post-metadata">

**Author:** ![KristinaLinville](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/919ad9/32.png) [@KristinaLinville](https://www.ruby-forum.com/u/KristinaLinville)\
**Post date:** [September 28, 2023, 9:16am UTC](https://www.ruby-forum.com/t/code-review-for-beginner/245321/21 "2023-09-28T09:16:44Z")

</div>

Thanks for the response.
