Home>

I am doing conditional branching with rails, but the array a below is narrowed down by where as shown in the second code, and there may or may not be contents. However, since 0 may be entered in a, we want to distinguish between 0 and nil and detect only nil (the contents of the array are empty).

<% @ thru_1h_start.zip (@ thru_10h_start) .each do | a, b |%>
    <% case%>
    <% when a.nil?%>
・ ・ ・
#Contents of variable a
@ thru_1h_start = Score.where (created_at: play_date.created_at.in_time_zone.all_day) .where (course: params [: course]) .where (hole_number: 1..9) .where ("hole_score is NULL") .group ( : round_id) .minimum (: hole_number)
@ thru_10h_start = Score.where (created_at: play_date.created_at.in_time_zone.all_day) .where (course: params [: course]) .where (hole_number: 10..18) .where ("hole_score is NULL") .group ( : round_id) .minimum (: hole_number)
What I tried

I also tried .any? And .empty ?, but the result was the same as .nil ?.

I want to achieve

If the contents of the array are empty, I want to set it to true in the when statement.

Thank you.

Postscript

Looking at the link below, it says that if the conditions are not met, it is not an empty array.

https://qiita.com/nakayuu07/items/3d5e2f8784b6f18186f2

  • Answer # 1

    If the contents of the array are empty, I want to set it to true in the when statement.

    p [] .empty? # =>true
    p [1, 2, 3] .empty? # =>false
    p [0] .empty? # =>false


    Execution result: https://paiza.io/projects/ywocFN_SSjiw7-xzBn4cXA?language=ruby
    empty method: https://docs.ruby-lang.org/ja/latest/method/Array/i/empty=3f.html

  • Answer # 2

    Is each data an array like the one below?

    @ thru_1h_start = [["683bc2ab3f8ece9d", 1], ["2656bd31891aaf04", 3]]
    @ thru_10h_start = [["683bc2ab3f8ece9d", 18], ["2656bd31891aaf04", 10], ["hoge", 11]]

    in this caseArray # zipCannot be synthesized by.

    @ thru_1h_start.zip (@ thru_10h_start)
    # =>[
    # [["683bc2ab3f8ece9d", 1], ["683bc2ab3f8ece9d", 18]],
    # [["2656bd31891aaf04", 3], ["2656bd31891aaf04", 10]]
    #]

    one timeHashAfter converting tomergeI think it is better to use or values_at.

    ha = Hash [@ thru_1h_start]
    hb = Hash [@ thru_10h_start]
    keys = ha.keys | hb.keys
    values_a = ha.values_at (* keys)
    values_b = hb.values_at (* keys)
    keys.zip (values_a, values_b) do | key, a, b |
    ...

    Note that "judgment from above"
    if a&&a.between? (1,18)&&b == 10
    And so on.
    However, since it becomes uselessly long, use a safe navigation operator.

    <span>
    <% if a.nil?&&b.nil?%>Finish
    <% elsif a.nil?&&b&.between? (11,18)%>Out-start, in-play<% = b -1%>
    <% elsif a.nil?&&b == 10%>Out start, out end<% = a%>
    <% elsif a == 1&&b.nil?%>In start, in end<% = a -1%>
    <% elsif a == 1&&b&.between? (11,18)%>In-start, in-play<% = b -1%>
    <% elsif a&.between? (2,9)&&b == 10%>Out-start, out-playing<% = a -1%>
    <% elsif a&.between? (1,9)&&b.nil?%>In-start, out-play<% = a -1%>
    <% else%>It ’s a strange way to put a score.
    <% end%></span><br>


    And so on.

    However, although it is nice to see this, it is inefficient and anxious to make the same judgment several times.


    When using pattern matching

    <span>
    <% case [a, b]
    <% in [nil, nil]%>Finish
    <% in [nil, 10]%>Out start, out end<% = a%>
    <% in [1, nil]%>In start, in end<% = a -1%>
    <% in [nil, 11..18 =>hole]%>Out-start, in-play<% = hole -1%>
    <% in [1, 11..18 =>hole]%>In-start, in-play<% = hole -1%>
    <% in [2..9 =>hole, 10]%>Out-start, out-playing<% = hole -1%>
    <% in [1..9 =>hole, nil]%>In-start, out-playing<% = hole -1%>
    <% else%>It ’s a strange way to put a score.
    <% end%></span><br>

    Etc. are possible, but it is difficult to use it regularly, such as a warning at 2.7.

Related articles