1

this might be a very vague question. But i am wondering if someone could translate this into pseudocode:

a = (1 + (bool ? rand(13) : 0)

Does it mean that a will become any value between 0-13 + 1? what is the purpose of the boolean value and the question mark?

loopy
  • 61
  • 4
  • 1
    _what is the purpose of the boolean value and the question mark?_ I think it serves no purpose, unless it's replaced with a variable . Otherwise is as you mentioned, a is equals to 1 plus a random number between 0 and 12 (inclusive). – Sebastián Palma Apr 18 '21 at 16:35
  • 1
    Thank you Sebastian! I'll change my post to make the question more clear. – loopy Apr 18 '21 at 16:39
  • See [ternary if](https://ruby-doc.org/core-3.0.0/doc/syntax/control_expressions_rdoc.html#label-Ternary+if) – Stefan Apr 19 '21 at 08:11
  • @loopy : We don't know if the code in question contains a "boolean" value, in particular since Ruby does not have a "boolean" datatype. We only know that `bool` is some variable or parameterless method, which is (due to the ternary-if-operator) evaluated in a boolean context, i.e. is regarded as _trueish_ or _falsy_ - the latter is the case if it has the value `false` or `nil`. – user1934428 Apr 19 '21 at 09:53

1 Answers1

2
  1. (true ? rand(13) : 0) mean (if true then rand(13) else 0 end)

if you have directly "true" in condition, the "else" is never called (is useless), you can write : a = 1 + rand(13) directly ;)

  1. rand(13) give random int between 0 and 12 ;) if you want "13" put rand(14) personally I always use range like this (all range is include, it's easier to understand) : rand(0..13)
Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
Matrix
  • 3,458
  • 6
  • 40
  • 76
  • 2
    Thank you so much! I changed it from a boolean variable to ```true``` just to simply the question. But thank you so much for your explanation! It cleared everything up :) – loopy Apr 18 '21 at 16:38