0
<%= m.members %>

This gives back this array below. If I want to get only last row of this array(Ray's record), how can I code?

[#<User id: 4, username: "John", age: 27, deleted_at: nil>, 
#<User id: 8, username: "Mike", age: 27, deleted_at: nil>,
#<User id: 13, username: "Ray", age: 27, deleted_at: nil>]

1 Answer 1

1
[#<User id: 4, username: "John", age: 27, deleted_at: nil>, 
#<User id: 8, username: "Mike", age: 27, deleted_at: nil>,
#<User id: 13, username: "Ray", age: 27, deleted_at: nil>].last


If we want to get the last and first elements as follows:
Ex: arr = [1,2,3,4]
$> arr.last => 4
$> arr.first => 1

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for reply:) I tried it before but it gives back <User:0x00000017d55298> :(
please write .inspect after that.
The default to_s method for Array is the same as inspect in Ruby 1.9. An individual element's to_s method may be different. You shouldn't be dumping raw records into your HTML anyway, but instead accessing specific properties. to_yaml or to_json often provides a more readable output.
Also: [1,2,3,4][-1] #=> 4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.