returning value from ternary operator

S

Sijo Kg

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
 
R

Robert Klemme

2009/4/16 Sijo Kg said:
Hi
=A0 =A0 I have

=A0 =A0def get_company_details_array
=A0 =A0c =3D nil
=A0 =A0arr =3D [] if c.nil?
=A0 =A0c.nil? ? =A02.times {arr.push("")} :['a','b']
=A0end

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

def get_company_details_array
['','']
end
=A0arr =3D ['abc'] + get_company_details_array
=A0puts arr.inspect

=A0 What I expected here is ["abc","",""] =A0 But I am getting error
array.rb:7:in `+': can't convert Fixnum into Array (TypeError)
=A0 =A0 =A0 =A0from array.rb:7

=A0 =A0 =A0 =A0 Could anybody please correct the code?

Hint: Fixnum#times returns the number.

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
S

Sijo Kg

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,944
Members
47,492
Latest member
gabbywilliam

Latest Threads

Top