Returning value from ternary operator


#1

Hi
I have

def get_company_details_array
c = nil
arr = [] if c.nil?
c.nil? ?  2.times {arr.push("")} :['a','b']

end
arr = [‘abc’] + get_company_details_array
puts arr.inspect

What I expected here is [“abc”,"",""] But I am getting error
array.rb:7:in `+’: can’t convert Fixnum into Array (TypeError)
from array.rb:7

     Could anybody please correct the code?

Thanks in advance
Sijo


#2

2009/4/16 Sijo Kg removed_email_address@domain.invalid:

Hi
I have

def get_company_details_array
c = nil
arr = [] if c.nil?
c.nil? ? 2.times {arr.push("")} :[‘a’,‘b’]
end

What weird logic is that? Basically you can replace the method with

def get_company_details_array
[’’,’’]
end

arr = [‘abc’] + get_company_details_array
puts arr.inspect

What I expected here is [“abc”,"",""] But I am getting error
array.rb:7:in `+’: can’t convert Fixnum into Array (TypeError)
from array.rb:7

    Could anybody please correct the code?

Hint: Fixnum#times returns the number.

Cheers

robert


#3

No
it is not weird logic since I have omitted some portion for ease of
reading
complete code like

def get_company_details_array
c = self.onboarding_company
c.nil? ? ["","","",""] :[c.name,c.web_site_url,c.email,c.phone]
end