B
B
Bogdan2017-07-22 14:00:16
Ruby on Rails
Bogdan, 2017-07-22 14:00:16

To add methods of object through conditions?

Hello. Is it possible somehow in Ruby to add methods to an object depending on the conditions. Like in the example? Thank you.

sort_field = 'data'
sort_order ='desc'

arr = [ {data: 'one'}, {data: 'bbb'} ]
aaa = arr
if sort_field
  .sort_by { | v | v[sort_field.to_sym] }
  if sort_order == 'desc'
    .reverse
end

puts aaa

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2017-07-23
@2ord

In ActiveRecord you can do this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question