# Need help converting from ternary operator?

**URL:** <https://www.ruby-forum.com/t/need-help-converting-from-ternary-operator/260164>\
**Category:** Uncategorized\
**Created:** [August 20, 2020, 12:23am UTC](https://www.ruby-forum.com/t/need-help-converting-from-ternary-operator/260164 "2020-08-20T00:23:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SMR9220](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@SMR9220](https://www.ruby-forum.com/u/SMR9220)\
**Post date:** [August 20, 2020, 12:23am UTC](https://www.ruby-forum.com/t/need-help-converting-from-ternary-operator/260164/1 "2020-08-20T00:23:47Z")

</div>

![QUESTION where to put p in second method](https://www.ruby-forum.com/uploads/default/original/2X/d/db09fe2901a3e15292db727952114794915f843f.png)

As per the screenshot, I used the ternary operator to write a method that splits an array into two, based on the condition if the number is even or odd. However, in writing it in the basic block layout, I don’t know where I need to put the “print even” and “print odd” in order for those two arrays to print out when the method is called.

---

<div class="post-metadata">

**Author:** ![specious](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@specious](https://www.ruby-forum.com/u/specious)\
**Post date:** [August 20, 2020, 8:15am UTC](https://www.ruby-forum.com/t/need-help-converting-from-ternary-operator/260164/2 "2020-08-20T08:15:45Z")

</div>

To answer the original question, “just before the final `end` statement”. But it hardly seems to be worth creating a separate array for the odds and evens, and you don’t really need the ternary at all.

```ruby
fives = [5, 10, 15, 20, 25, 30, 35, 40]

def print_evens_and_odds(ary)
  p ary.select(&:even?)
  p ary.select(&:odd?)
end

print_evens_and_odds fives

```

PS. please don’t post screenshots, as it makes it harder for people to help you if they can’t cut and paste your code…

---

<div class="post-metadata">

**Author:** ![SMR9220](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@SMR9220](https://www.ruby-forum.com/u/SMR9220)\
**Post date:** [August 20, 2020, 9:48am UTC](https://www.ruby-forum.com/t/need-help-converting-from-ternary-operator/260164/3 "2020-08-20T09:48:20Z")

</div>

The challenge was to separate one array into odds and evens. And the ternary was my own default coding style (i know the block method is easier) idk why but that was my brain did and i just wanted to make sure ci knew how to write it both ways. Thank you for your help

---

<div class="post-metadata">

**Author:** ![sksamuel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@sksamuel](https://www.ruby-forum.com/u/sksamuel)\
**Post date:** [August 20, 2020, 10:16pm UTC](https://www.ruby-forum.com/t/need-help-converting-from-ternary-operator/260164/4 "2020-08-20T22:16:40Z")

</div>

To answer your question based on what code you’ve implemented, think of the scoping. Basically if you create the Even array and the Odd array at the beginning of your function, and then you edit those arrays by adding onto it using an each do loop, you need for your editing loop to finish so you can print out the final results of your array.each loop. Basically before the function ends and after the each do loop! I know it was answered previously but just to go more in depth with the example you’re given.

If you want more confidence in your own answer and being able to conceptually understand it, I would suggest looking into Scopes/Scoping.

---

<div class="post-metadata">

**Author:** ![specious](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@specious](https://www.ruby-forum.com/u/specious)\
**Post date:** [August 21, 2020, 7:12am UTC](https://www.ruby-forum.com/t/need-help-converting-from-ternary-operator/260164/5 "2020-08-21T07:12:24Z")

</div>

I must admit to having a bit of a soft spot for the ternary operator, although many people don’t like it (the designers of `Go` for example 😂). But in this case, the two arrays are only transient and are discarded once the function ends, so just passing an expression to the `p` function seemed like the better option. This doesn’t prevent the intermediate arrays from being created of course, but it happens under the covers rather than explicitly in the code.
