-2

In my rails project I've if statement with string conditions which doesn't work.

  if shop.details['company_role'] == 'CEO' || 'founder'
        'Other'
      else
        shop.details['customer_kind']
      end
  end

When I've case with company_role = founder it didn't return me 'Other' but customer_kind

mr_muscle
  • 2,536
  • 18
  • 61
  • You better look at the warning you should receive ` 2.6.2 :002 > a = 'test' => "test" 2.6.2 :003 > if a == 'one' || 'two' 2.6.2 :004?> puts 'Other' 2.6.2 :005?> else 2.6.2 :006?> puts 'Else' 2.6.2 :007?> end (irb):7: warning: string literal in condition Other => nil 2.6.2 :008 > if a == 'one' || false 2.6.2 :009?> puts 'Other' 2.6.2 :010?> else 2.6.2 :011?> puts 'Else one' 2.6.2 :012?> end Else one => nil ` – Yurii Verbytskyi May 29 '19 at 11:03
  • And than to https://stackoverflow.com/questions/21060234/ruby-operator-precedence-table ruby precedence table(`==` runs first, than you take if false || 'founder') – Yurii Verbytskyi May 29 '19 at 11:06
  • @YuriyVerbytskyi pasting irb sessions in comments rarely works. – Stefan May 29 '19 at 11:27
  • @Stefan oh yeah, but it doesn't looks like anybody but PO will be interested in that as an answer( – Yurii Verbytskyi May 29 '19 at 14:27

5 Answers5

2

The premise is a little off. The if statement works, but it's not producing the results that you expect. It's basically doing the equivalent of this:

shop.details['company_role'] == 'CEO' # is this true? then stop | "founder" # is this true? yes, it's always truthy

The solutions provided will all work. Your choice mostly depends on your code style. I'd go with the second option because I personally like the test with an emphasis on the tested data.

Two further suggestions. First, the test itself could be extracted to a method. This would improve readability of your code.

def shop_type
  if executive?
    'Other'
  else
    shop.details['customer_kind']
  end
  # alternate: executive? ? 'Other' : shop.details['customer_kind']
end

def executive?
  shop.details['company_role'].in?('CEO','founder')
end

Lastly, it strikes me that the decision about whether or not a Shop includes an "executive?" seems to be useful information in a number of contexts. As such, it looks like the executive? method would be better placed inside the Shop model so that knowledge of the internal organization of the Shop does not leak all over your system.

shop.executive? ? 'Other' : shop.details['customer_kind']

class Shop
  ...
  def executive?
    shop.details['company_role'].in?('CEO','founder')
  end
end
AndyV
  • 3,696
  • 1
  • 19
  • 17
1

Try like this,

if ['CEO','founder'].include?(shop.details['company_role'])
  'Other'
else
  shop.details['customer_kind']
end
1
%w(CEO founder).include?(shop.details['company_role']) ? 'Other' : shop.details['customer_kind']
zlei1
  • 19
  • 1
1
if shop.details['company_role'].in?('CEO','founder')
  'Other'
else
  shop.details['customer_kind']
end
Ashish Garg
  • 105
  • 1
  • 9
0

Keep it simple-

if shop.details['company_role'] == 'CEO' || shop.details['company_role'] == 'founder'
        'Other'
else
        shop.details['customer_kind']
end
sm83
  • 15
  • 6