Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question