Why Won't Ternary Operator Work with Redirect

Why won't ternary operator work with redirect

Because of the implied nature of the hash

Ruby / Rails will imply your argument to redirect is a hash which has some awkward implications in your example

When ruby implies the arguments to redirect it sees it as a hash in the following scenario it is parsing it as

nexti==0 ? redirect_to({:back, : redirect_to edit_playt_path(id: actform['playt_id'], i: nexti})

which is invalid, it should work if you explicitly define your hash / argument

nexti==0 ? redirect_to(:back) : redirect_to(edit_playt_path({id: actform['playt_id'], i: nexti}))

most ruby / rails devs will tell you to avoid ternaries for reasons like this, and also general human comprehension of what is going on. Ruby was ment to be an expressive language, so embrace it.

return redirect_to(:back) if nexti==0
redirect_to(edit_playt_path({id: actform['playt_id'], i: nexti}))

Why can't I use the ternary operator inside a case in ruby?

You're missing the ? operator:

when partner_x_url then Rails.env.development? ? partner_signup_url : partner_signup_url :protocol => "https"

Happens to me all the time with a method ending in a question mark :)

EDIT: Since you now added that operator, I suspect it has to do with operator precedence. Try to avoid those kind of issues with using parentheses around function arguments:

when partner_x_url then Rails.env.development? ? partner_signup_url : partner_signup_url(:protocol => "https")

Example: Without function parentheses, you get a syntax error:

1.9.3p125 :006 > case @foo when true then true ? puts "foo" : puts "bar" end
SyntaxError: (irb):6: syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('
case @foo when true then true ? puts "foo" : puts "bar" end
^ ^

With parentheses, you don't:

1.9.3p125 :007 > case @foo when true then true ? puts("foo") : puts("bar") end
=> nil

Why do I get an error using a ternary operator inside a hash literal?

There is no problem in using the ternary operator in a hash literal definition.

In x==nil? the interpreter is considering the ? along with nil as a call to the nil? method. So what would be the rest of the ternary operator is effectively a syntax error, since you don't really have a well formed operator because the ? is not part of it.

What you should have entered, by using proper spacing, is:

h = {:a => x == nil ? "" : x}

Which can be better expressed as:

h = {:a => x.nil? ? "" : x}

If false is not a valid value for x you can also use the form h = {:a => x || ""} as suggested by @Gareth

Also, if x is supposed to always be a string, you can use the form h = {a: x.to_s} as suggested by @sawa

Rails one line conditional failing with syntax error

You have to use ( and ) because ruby cannot recognise what exactly do you mean

unless current_user 
redirect_f ? redirect_to(root_url) : return(redirect_f)
end

Using ternary operator in JavaScript to invoke two functions

Yes, that's valid code. It will invoke either function1() or function2(), but not both - depending on the value of type.

How to avoid nested ternary operators

One option is to make an IIFE:

{
(() => {
if (isDataLoading) return (<MyLoadingComponent />);
if (!products) return (<ThereIsNoDataComponent />);
return (<div>Some text</div>);
})()
}

Or, if you want to avoid re-creating the function every time:

const render = (isDataLoading, products) => {
if (isDataLoading) return (<MyLoadingComponent />);
if (!products) return (<ThereIsNoDataComponent />);
return (<div>Some text</div>);
};

and

{ render(isDataLoading, products) }

How to write ternary operator condition in jQuery?

I would go with such code:

var oBox = $("#blackbox");
var curClass = oBox.attr("class");
var newClass = (curClass == "bg_black") ? "bg_pink" : "bg_black";
oBox.removeClass().addClass(newClass);

To have it working, you first have to change your CSS and remove the background from the #blackbox declaration, add those two classes:

.bg_black { background-color: #000; }
.bg_pink { background-color: pink; }

And assign the class bg_black to the blackbox element initially.

Updated jsFiddle: http://jsfiddle.net/6nar4/17/

In my opinion it's more readable than the other answers but it's up to you to choose of course.

Using ternary operator in echo?

The ?? Operator in PHP is the Null Coalescing Operator:

expr1 ?? expr2;

expr1 is returned when expr1 exists and is NOT null; otherwise it returns expr2.

Since in this case expr1 is false but is set, this expression returns Boolean false.

Compare:

echo false ?? 'It is FALSE'; // won't be displayed
echo null ?? 'it is NULL'; // It will work

Echo does not output when passed Boolean false.



Related Topics



Leave a reply



Submit