[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Mapping an object

Harnish Botadra

10/5/2007 12:18:00 AM

Hi,

This is Harnish. I am looking for a way to map an object.

Ex:

Instead of doing,

resultset = A.get_arr
result = resultset[0].my_obj


Can I combine the 2 statements into 1 when I know that my A.get_arr is
sure to return an array with just 1 object.

Thanks for your help.

Regards,
Harnish
--
Posted via http://www.ruby-....

2 Answers

Harold Hausman

10/5/2007 12:27:00 AM

0

On 10/4/07, Harnish Botadra <harnish_544@yahoo.com> wrote:
> Instead of doing,
>
> resultset = A.get_arr
> result = resultset[0].my_obj
>
>
> Can I combine the 2 statements into 1 when I know that my A.get_arr is
> sure to return an array with just 1 object.
>

I think your code could just become:
result = A.get_arr[0].my_obj

Consider this:
irb(main):002:0> def foo; ["foo"]; end
=> nil
irb(main):003:0> foo[0].reverse
=> "oof"

Hope that helps,
-Harold

Konrad Meyer

10/5/2007 1:06:00 AM

0

Quoth Harnish Botadra:
> Hi,
>
> This is Harnish. I am looking for a way to map an object.
>
> Ex:
>
> Instead of doing,
>
> resultset = A.get_arr
> result = resultset[0].my_obj
>
>
> Can I combine the 2 statements into 1 when I know that my A.get_arr is
> sure to return an array with just 1 object.
>
> Thanks for your help.
>
> Regards,
> Harnish

result = A.get_arr.first # => First object of A.get_arr's array.

--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertil...