Ruby Ternary Operator and Method Call

Is the ternary operator ?: defined as a method in Ruby?

Is the ternary operator in Ruby really just a syntactic sugar for if ... then ... else ... end statements?

Yes.

From doc/syntax/control_expressions.rdoc

You may also write a if-then-else expression using ? and :. This ternary if:

input_type = gets =~ /hello/i ? "greeting" : "other"

Is the same as this if expression:

input_type =
if gets =~ /hello/i
"greeting"
else
"other"
end

"According to this book, "every operation is a method call on some object and returns a value." In this sense, if the ternary operator represents an operation, it is a method call on an object with two arguments."

if, unless, while, and until are not operators, they are control structures. Their modifier versions appear in the operator precedence table because they need to have precedence in order to be parsed. They simply check if their condition is true or false. In Ruby this is simple, only false and nil are false. Everything else is true.

Operators are things like !, +, *, and []. They are unary or binary. You can see a list of them by calling .methods.sort on various objects. For example...

2.4.3 :004 > 1.methods.sort
=> [:!, :!=, :!~, :%, :&, :*, :**, :+, :+@, :-, :-@, :/, :<, :<<, :<=, :<=>, :==, :===, :=~, :>, :>=, :>>, :[], :^, :__id__, :__send__, etc...

Note that in Smalltalk, from which Ruby borrows heavily, everything really is a method call. Including the control structures.

Ruby ternary operator and method call

When you use the shorthand syntax (without brackets), ruby expects everything until the end of the line to be parameters to your method. So your "syntax error" example is understood as:

request.xhr?  ? render(:json => "success"  : redirect_to index_url)

which is obviously wrong.

Ruby ternary operator (or) or operator

The difference here is that, for example, [].present? or ''.present? both return false. So:

question = ''
@question = question.present? ? question : Question.new
# => result of Question.new
@question = question || Question.new
# => ''

But it shouldn't mean anything in your case if question can only hold nil or Question instance (which is always present, assuming it's a regular ActiveRecord model). So it's more a matter of personal taste.

In Ruby, why doesn't ternary operator work when using a puts function inside an each statement?

This is caused by ambiguous arguments. Ruby, unlike many other languages such as JavaScript, is really lax about requiring brackets puts x and puts(x) are equivalent. Where this breaks down is when it's not clear which are arguments, and which are part of other syntax.

For example, if f is a method that takes 1-3 arguments and x and y are variables, then what is the meaning of:

f x, f x, y

Is that f(x, f(x,y)), f(x), f(x), y or f(x, f(x), y)? Ruby throws an error rather than presume the wrong thing.

Easy fix is to eliminate the duplication of effort here and use a single puts with a ternary used to determine the value:

array = [1,2,3]
array.each { |x| puts x > 2 ? "lower" : "higher"}

Now it works because there's no ambiguity on arguments, there's only one method call so it's easy.

Is it possible to put the ternary operator inside a function call?

it might be easier to make a custom helper for yourself - something like this:

def link_as_image(image)
image = generic_image if image.blank?
path = path_for image

link_to image_tag(image), path
end

def path_for(image)
current_user == image.user ? edit_image_path(image) : image_path(image)
end

Ruby - Is it possible to use a ternary operator in a proc?

Try:

capitalizer = Proc.new { |word| little_words.include?(word) ? word : word.capitalize }

That should work.

How can I use the ternary operator with multiple values?

Since a ternary operator is exactly that (an operator), you can't split it over multiple instructions.

However, blocks make it possible to "squash" multiple instructions into a single expression.

def my_withdraw(pin_number,amount)
puts (
pin_number == @pin ?
begin
@balance -= amount
"Withdrew #{amount}."
end :
pin_error
)
end

Of course, this is completely illegible and I would never recommend you use that sort of syntax in the real world. But for the sake of science, why not.

Ruby ternary operator without else

a.action if object.method?


Related Topics



Leave a reply



Submit