[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

returning value from ternary operator

Sijo Kg

4/16/2009 10:31:00 AM

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
--
Posted via http://www.ruby-....

2 Answers

Robert Klemme

4/16/2009 10:53:00 AM

0

2009/4/16 Sijo Kg <sijo@maxxion.com>:
> 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.rubybestprac...

Sijo Kg

4/16/2009 11:22:00 AM

0

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

--
Posted via http://www.ruby-....