Forum: Ruby returning value from ternary operator

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-04-16 12:30
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-04-16 12:53
(Received via mailing list)
2009/4/16 Sijo Kg <sijo@maxxion.com>:
> 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
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-04-16 13:22
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
This topic is locked and can not be replied to.