Reputation: 21
I'm having a little difficulty in understanding the below under initialize, and color methods. This is from the solution I was given:
The one under initialize: I think it means @given equals value, and if value is equal to 0, then its false, otherwise its true
Under color not sure what the 2nd question mark means.
def initialize(value)
@value = value
@given = value == 0 ? false : true
end
def color
given? ? :blue : :red
end
def given?
@given
end
Upvotes: 2
Views: 69
Reputation: 136
Mostly we use ? mark as a part of the method's name which returns boolean value. this is just a convention we ruby coders use to follow.
In your example given? is the method name as it return boolean value so here we have follow the convention.
and in ternary conditional operator we use
condition ? do something when condition is true : do something when condition is false
In your case given? it's returning true or false and which is also method name
So the first question mark is a part of the method name and 2nd question mark is the part of ternary condition
Upvotes: 1
Reputation: 369623
Under color not sure what the 2nd question mark means.
This is called the conditional operator. (See, for example, section 11.5.2.2.5 Conditional operator expression of the ISO Ruby Language Specification.)
condition ? consequence_truthy : consequence_falsy
will evaluate condition
and depending on whether the result is truthy or falsy, it will evaluate either the first or the second consequence.
Personally, I find the conditional operator useless in Ruby. In C and similar languages, the conditional operator is required because it is an expression, whereas the conditional statement is, well, a statement. So, if you need to use a conditional in an expression, you cannot use a conditional statement, you must use the conditional operator.
However, this does not apply to Ruby: in Ruby, everything is already an expression, there are no statements. The conditional expressions (see, for example, section 11.5.2.2.2 The if
expression, section 11.5.2.2.3 The unless
expression, and section 11.5.2.2.4 The case
expression) can already be used as an expression, so there is no need to use the conditional operator.
Personally, I find the conditional expression more readable than the conditional operator:
if condition then consequence_truthy else consequence_falsy end
This is semantically equivalent to the conditional operator, the only difference is syntactical: it has different precedence, which however, actually works more natural than the precedence of the conditional operator.
So, your examples could, in my opinion, be clearer written as
def initialize(value)
@value = value
@given = if value == 0 then false else true end
end
def color
if given? then :blue else :red end
end
Actually, the initialize
contains a useless-use-of-conditional because Integer#==
already returns a boolean. So, the following would be even clearer:
def initialize(value)
@value = value
@given = value != 0
end
Upvotes: 3