Skip to main content
added 16 characters in body
Source Link
aherve
  • 181
  • 4

Ruby

This is a super efficient piece of code that simulates a dice.

def almost_dice
  rand(7) # will output a number between 0 and 6. 
end

#I want a dice that outputs a number between 1 and 6, so I have no other choice, right?
def proper_dice
  d = almost_dice
  while d = 0
    d = almost_dice #Eventually, I'll get d != 0
  end
  return d
end

puts proper_dice

In ruby what could look like a test : d = 0 is actually an assignment. Since the assignement always succeed, then the test always returns true, hence the infinite loop

Ruby

This is a super efficient piece of code that simulates a dice.

def almost_dice
  rand(7) # will output a number between 0 and 6. 
end

#I want a dice that outputs a number between 1 and 6, so I have no other choice, right?
def proper_dice
  d = almost_dice
  while d = 0
    d = almost_dice #Eventually, I'll get d != 0
  end
end

puts proper_dice

In ruby what could look like a test : d = 0 is actually an assignment. Since the assignement always succeed, then the test always returns true, hence the infinite loop

Ruby

This is a super efficient piece of code that simulates a dice.

def almost_dice
  rand(7) # will output a number between 0 and 6. 
end

#I want a dice that outputs a number between 1 and 6, so I have no other choice, right?
def proper_dice
  d = almost_dice
  while d = 0
    d = almost_dice #Eventually, I'll get d != 0
  end
  return d
end

puts proper_dice

In ruby what could look like a test : d = 0 is actually an assignment. Since the assignement always succeed, then the test always returns true, hence the infinite loop

code typo
Source Link
aherve
  • 181
  • 4

Ruby

This is a super efficient piece of code that simulates a dice.

def almost_dice
  rand(7) # will output a number between 0 and 6. 
end

#I want a dice that outputs a number between 1 and 6, so I have no other choice, right?
def proper_dice
  d = almost_dice
  while d = 0
    d = almost_dice #Eventually, I'll get d != 0
  end
end

puts diceproper_dice

In ruby what could look like a test : d = 0 is actually an assignment. Since the assignement always succeed, then the test always returns true, hence the infinite loop

Ruby

This is a super efficient piece of code that simulates a dice.

def almost_dice
  rand(7) # will output a number between 0 and 6. 
end

#I want a dice that outputs a number between 1 and 6, so I have no other choice, right?
def proper_dice
  d = almost_dice
  while d = 0
    d = almost_dice #Eventually, I'll get d != 0
  end
end

puts dice

In ruby what could look like a test : d = 0 is actually an assignment. Since the assignement always succeed, then the test always returns true, hence the infinite loop

Ruby

This is a super efficient piece of code that simulates a dice.

def almost_dice
  rand(7) # will output a number between 0 and 6. 
end

#I want a dice that outputs a number between 1 and 6, so I have no other choice, right?
def proper_dice
  d = almost_dice
  while d = 0
    d = almost_dice #Eventually, I'll get d != 0
  end
end

puts proper_dice

In ruby what could look like a test : d = 0 is actually an assignment. Since the assignement always succeed, then the test always returns true, hence the infinite loop

Source Link
aherve
  • 181
  • 4

Ruby

This is a super efficient piece of code that simulates a dice.

def almost_dice
  rand(7) # will output a number between 0 and 6. 
end

#I want a dice that outputs a number between 1 and 6, so I have no other choice, right?
def proper_dice
  d = almost_dice
  while d = 0
    d = almost_dice #Eventually, I'll get d != 0
  end
end

puts dice

In ruby what could look like a test : d = 0 is actually an assignment. Since the assignement always succeed, then the test always returns true, hence the infinite loop