-4

Looking for a way to include a ternary conditional inside a hash assignment.

a = 5
h = {}
h[:alpha] => a > 3 ? true : false  # edited twice
h[:alpha] => (a > 3 ? true : false)    # edited twice

There has to be a way of shortening this up.

Rich_F
  • 1,830
  • 3
  • 24
  • 45
  • Did you mean `h[:alpha] = (a > 3 ? true : false)` ? – eugen Jan 20 '16 at 21:23
  • Yes I did, sorry. I started this question before I fixed that. – Rich_F Jan 20 '16 at 21:24
  • 1
    What do you mean by "doesn't work"? Both code snippets assign `true` to `h[:alpha]`. What is the behavior you are getting? What is the behavior you are expecting? What is the exact error message you are getting? – Jörg W Mittag Jan 20 '16 at 23:15
  • It's been edited from the original post. I was told to edit original posts even though it would be confusing. It was h[:alpha] => a > 3... – Rich_F Jan 20 '16 at 23:17
  • 1
    What is your question then? You say you are looking for a way to use a ternary, then you show a way to use a ternary, so, what is it you're looking for? – Jörg W Mittag Jan 21 '16 at 00:37
  • It has been solved. I was having string interpolation issues, not ternary. – Rich_F Jan 21 '16 at 00:37
  • 1
    Editing your question in a way that invalidates existing answers is *extremely* rude. It invalidates the hard work people have been putting into their answers, and will lead to their answers being downvoted for being wrong. – Jörg W Mittag Jan 21 '16 at 00:38
  • Yes, I agree. But then I get the "holier than thou" developers in here giving me guff because I don't edit my original post. So if you have a problem with that, then chase them, not me. So in essence, everybody that posts a question, is wrong. You know what? Just because of you, I'll set it back. Expect someone to come in and say I should edit it. – Rich_F Jan 21 '16 at 00:40
  • 1
    The reason why you should edit your question is because it is massively unclear what it even is that you are asking. The title says that you are asking about the ternary operator but you just now told me that you are actually having a problem with string interpolation, yet there is no mention of string interpolation anywhere in the question. In the question, you say you are looking for a way of shortening up the code you posted, but in a comment to sawa's answer, which shows exactly that, you say that it *doesn't* answer your question. That is a confusion you need to clear up by editing your … – Jörg W Mittag Jan 21 '16 at 00:47
  • … question, but *not* by turning it into a completely different question that invalidates existing answers. – Jörg W Mittag Jan 21 '16 at 00:48
  • Read what I put. Also later on in the comments I published the actual application where it still wasn't working. So read the page before you start lecturing. Now you're telling me to edit the question after telling me not to edit the question? Yes I thought it was ternary and not string interpolation. Hence the post. I also stated that it was string interpolation that was the issue, and not the ternary. Next time you should read before getting on your high horse. – Rich_F Jan 21 '16 at 00:50
  • And it doesn't invalidate anything. The problem turned out to be something else, as is a lot of cases in coding. Try to keep up. – Rich_F Jan 21 '16 at 00:50
  • 1
    I didn't tell you anything. I politely reminded you that if you want other people to do *your* work for free, it would be nice if you could put in the effort of editing the question into a shape where it can be answered without having to play a game of 20 questions, in order to *clarify* your question but not *change* it, because if you *change* the question midway through, then all the effort that people have spent in order to do *your* work *for free* would be wasted. – Jörg W Mittag Jan 21 '16 at 01:02

2 Answers2

4

Almost always when a beginner is using a literal true or false, that is unnecessary. In this case, you don't need a ternary at all.

a = 5
h = {}
h[:alpha] = a > 3
h[:alpha] # => true
sawa
  • 165,429
  • 45
  • 277
  • 381
  • That was to test a compound ternary. The actual case is different, which is in the comments above. – Rich_F Jan 20 '16 at 21:36
  • 4
    @Rich_F Don't put important information in a comment. Add it to your question to make it clear. – sawa Jan 20 '16 at 21:37
  • @Rich_F also compound ternary makes for code smell. It is difficult to read and interpret if the logic needs more than an if else then ternary is not the best solution and you are better off writing a method to handle this. – engineersmnky Jan 20 '16 at 21:47
  • 2
    This is the correct answer. This should be the accepted one, not the other one which contains a `SyntaxError` and doesn't even parse properly. – Jörg W Mittag Jan 21 '16 at 00:50
3

You need to assign the values by using = (assignment operator) and not =>.

Try:

h[:alpha] = a > 3 ? true : false

Example:

2.1.2-perf :001 > a = 5
 => 5
2.1.2-perf :002 > h = {}
 => {}
2.1.2-perf :005 > h[:alpha] = (a > 3 ? true : false)
 => true
2.1.2-perf :006 > h[:alpha]
 => true

Edit (As per your comments):

2.1.2-perf :014 > user = [1,2,3,4,5]
 => [1, 2, 3, 4, 5]
2.1.2-perf :016 > user[1] == "solo" ? "#{user[2]} #{user[3]} (s)" : "#{user[4]} (g)"
 => "5 (g)"
Atri
  • 5,511
  • 5
  • 30
  • 40
  • Here is my current issue, on different syntax: {:type => user[1], :name => user[1].to_s == "solo" ? "#{ user[2] user[3] (s)} " : "#{ user[4] (g)}", – Rich_F Jan 20 '16 at 21:27
  • you need to use `"#{user[2]} #{user[3]} (s)"` if this is what you are expecting – Atri Jan 20 '16 at 21:30
  • Changed it to that, and it's hanging up on #{user[2]}. user[1] == "solo" ? puts "#{user[2]} #{user[3]} (s)" : puts "#{user[4]} (g)" – Rich_F Jan 20 '16 at 21:31
  • remove `puts` from the ternary – Atri Jan 20 '16 at 21:33
  • Done. I think it was my string interpolation slip. Thanks. – Rich_F Jan 20 '16 at 21:37
  • Downvoted. The first line of code isn't even syntactically valid. And the last part is completely unrelated to the question. – Jörg W Mittag Jan 21 '16 at 00:58
  • @JörgWMittag Thanks for pointing out the typo. The last part is based on OP's comments. OP needs to update question with the required details. – Atri Jan 21 '16 at 01:26