简体   繁体   中英

RoR free element from array

i have an element in a array such that,

[#<Name id: 23, first_name: "anderson", last_name: "gorge">]

how can i remove the array block, so that i want it as

#<Name id: 23, first_name: "anderson", last_name: "gorge">

without array block

Do you mean you have a single element in an array and you want to extract it? There so many ways of doing it, 2 that quickly come to mind are:

>> obj = [#<Name id: 23, first_name: "anderson", last_name: "gorge">].first
=> #<Name id: 23, first_name: "anderson", last_name: "gorge">
>> obj = [#<Name id: 23, first_name: "anderson", last_name: "gorge">].shift
=> #<Name id: 23, first_name: "anderson", last_name: "gorge">
irb(main):005:0> arr = ["#"]
irb(main):006:0> elem = arr.first
irb(main):008:0> elem == "#"
=> true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM