Code review for beginner

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

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

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.

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.

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.

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?

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 …

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

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

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

Reach :

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.

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

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

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:

⮚ 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

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. Will be waiting for more content like this from here.

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 provides by leading IT engineers, who have work experience for a decade in the domain.

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

Thanks for the response.