Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternary expression with "defined?" returns "expression" instead of value?

I'm pretty new to Ruby and Rails but even after searching stack overflow and google I couldn't find an answer to this.

I've got a simple Ruby shorthand if statement that should return an integer like so:

# in the context of this erb document `amount` is defined as 5.
@c = ( defined? amount ? amount : r( 1,4 ) )

r() is a custom helper function that returns a random number between in this case 1 and 4.

The way I intend this to work is that if amount is defined, then use the number defined as amount, else generate a random number between 1 and 4 and use that instead.

When printing out @c however Ruby outputs expression rather than a number.

What do I have to do to get this working as I intended and what am I doing wrong?

Many thanks for reading!

like image 1000
Jannis Avatar asked May 06 '12 01:05

Jannis


People also ask

Can we use return statement in ternary operator?

Explanation: In a ternary operator, we cannot use the return statement.

How do you use ternary operator for 3 conditions?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

Is ternary operator Pythonic?

The ternary operator is a way of writing conditional statements in Python. As the name ternary suggests, this Python operator consists of three operands. The ternary operator can be thought of as a simplified, one-line version of the if-else statement to test a condition.

What is a ternary operator explain with the help of an example?

The ternary operator is an operator that exists in some programming languages, which takes three operands rather than the typical one or two that most operators use. It provides a way to shorten a simple if else block. For example, consider the below JavaScript code. var num = 4, msg = ""; if (num === 4) {


2 Answers

defined? is binding to amount ? amount : r(1,4) so it is equivalent to:

defined?(amount ? amount : r(1,4))

You probably want:

defined?(amount) ? amount : r(1,4)

Actually, odds are that amount || r(1,4), or amount.nil? ? r(1,4) : amount would better match what you want, since I think you don't want this:

1.9.3p194 :001 > defined?(amount)
 => nil 
1.9.3p194 :002 > amount = nil
 => nil 
1.9.3p194 :003 > defined?(amount)
 => "local-variable" 

...in which case @c would be nil - the value of the defined variable.

like image 101
Daniel Pittman Avatar answered Nov 13 '22 13:11

Daniel Pittman


Use the || operator in this case:

@c = amount || r (1,4)

In your code, the defined? method operates on amount ? amount : r( 1,4 ) instead of just on amount as you intended. Also, the defined? operator probably doesn't do what you expect, have a look at this blog entry to get an idea.

like image 32
Femaref Avatar answered Nov 13 '22 12:11

Femaref