lundi 6 mai 2019

How to use correctly def with if..else statements in ruby?

I started learning ruby, for this moment I understand how to create class objects, and how to write very simple IF..ELSE statement. But how to write correctly def with IF..ELSE statement? Help please with advice how to write it correctly?

   #I can write simple statement like this 

number = 1
if number == 1
    number += 1
    puts "the number is #{number}"
else 
    puts "number is more then 2"
end

#But then i want to write something like this 

class Maths
  def initialize(number_id)
    @number = number_id 
  end 
    def numberdata()
      if @number == 1 
         @number +=1
      puts "the number is #@number"
    else 
      puts "number is greater than 3"
   end
  end
end
   classob5 = Maths.new("1")
   classob5.numberdata()`

I expected the outout from the second part like this "the number is 2", but actual is "number is greater than 3" Help please with advice how to write it correctly

Aucun commentaire:

Enregistrer un commentaire