Return error message when split by 0 - Ruby

Asked

Viewed 108 times

-2

All right? I’m doing a challenge on Ruby and the def’s need to be the way it is in the code I’m gonna send. The division by 0 needs to return with the message "Oops! Zero as divisor" I’ve been trying for a while but I still can’t get this result, always presents me the mistake

An error occurred while loading ./spec/calculadora_spec.rb.
Failure/Error: require 'calculadora'

SyntaxError:
  /home/leticia/Documentos/campos_code/calculator/lib/calculadora.rb:18: syntax error, unexpected `end', expecting end-of-input
# ./spec/calculadora_spec.rb:2:in `require'
# ./spec/calculadora_spec.rb:2:in `<top (required)>'
No examples found.

Finished in 0.00004 seconds (files took 0.26173 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

My code is like this:

def soma(primeiro_numero, segundo_numero)
    primeiro_numero+segundo_numero
end

def subtracao(primeiro_numero, segundo_numero)
    primeiro_numero-segundo_numero
end

def multiplicacao(primeiro_numero,segundo_numero)
    primeiro_numero*segundo_numero
end

def divisao(primeiro_numero,segundo_numero)
    primeiro_numero / segundo_numero
    end
ZeroDivisionError
    puts "Opa! Zero como divisor"
end

Can you give me a hand? I made some changes as well and my last code became so

    def divisao(primeiro_numero,segundo_numero)
if segundo_numero != 0
    return primeiro_numero/segundo_numero
elsif primeiro_numero = 0
    puts 'Opa! Zero como divisor'
else segundo_numero = 0
    puts 'Opa! Zero como divisor'
end

But it really doesn’t work yet. I’ve already looked at the course booklets, videos on Youtube, I asked the fellow and nothing, really stuck

3 answers

2

After a lot of talk and code, we were able to get the result. It needs an extra end at the end of the last function. From what we can understand, one end "closes" the if and the other end "closes" the function The code went like this:

def soma(primeiro_numero, segundo_numero)
    primeiro_numero+segundo_numero
end

def subtracao(primeiro_numero, segundo_numero)
    primeiro_numero-segundo_numero
end

def multiplicacao(primeiro_numero,segundo_numero)
    primeiro_numero*segundo_numero
end

def divisao(primeiro_numero,segundo_numero)
    if segundo_numero != 0
        return primeiro_numero/segundo_numero
    else 
        return 'Opa! Zero como divisor'
    end
end

2

Interesting to use the idea of "Guard Clause"

def divisao(primeiro_numero, segundo_numero)
  return 'Opa! Zero como divisor' if segundo_numero == 0 
  primeiro_numero / segundo_numero
end

0

def divisao(primeiro_numero,segundo_numero)
    begin
        puts primeiro_numero / segundo_numero
    rescue ZeroDivisionError
        puts "Opa! Zero como divisor"
    end
end

divisao(10,2) # =>  5
divisao(10,0) # => Opa! Zero como divisor
  • Try to explain your answer better...

  • @Edumendonça Why don’t you try to help the person with the problem, instead of negativizing my answer? Here’s the hypocrisy!

  • You are not, I have been denied several times for giving answers without explanation, I do not want other people to go through the same problem and another is not the purpose of stackoverflow.com to leave an answer without a logical explanation. your code can answer the question but the people who receive the answer will be left with no reason why this code should be used for it. It is the same as answering "why yes."...

  • "I don’t want other people to go through the same trouble". Then you go in there and do the same thing they did unjustly to you, to me. I was just gonna tap the comments, man, here’s the tip!

  • Old is not me, it is the system of pt.stackoverflow.com I just analyze like any other that can do, and other I did not negatively I just signal as low quality if someone negatived neither I and nor you will know who was...

  • @Edumendonça in this I will be forced to agree with Maicon Santos. Do with the colleague what you do not like to be done with you does not make much sense.

  • Guys I just told him to explain the answer better and not only leave a code there as answer I his answer appeared in the analysis queue and I commented I did not vote negative I just commented as all other people do with me... when I leave a code loose... without reply...

  • When you have enough points to do analysis of the questions and answers you will understand....

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.