Hi,
I'm new to Ruby and am trying to pass a sort_by lambda to a format method, like this:
sort_by_methods = [ lambda {|l, r| compare_by_gender_then_last_name(l, r)},
lambda {|l, r| compare_by_something_else(l, r)},
lambda {|l, r| compare_by_another(l, r)}]
formatted_output = ""
sort_by_methods.each do |sort_by|
formatted_output << formatter.format(students) { sort_by }
end
The format method code looks something like:
def format(students, &sort_by)
sorted_students = students.sort { |l, r| sort_by.call(l, r) } // error from this line
sorted_students.each { |s| result << s.to_s << "\n" }
end
For some reason I am getting a interpreter complaint about the line in the above format method code (students.sort.....):
"in sort': undefined method
>' for # (NoMethodError)"
What am I doing wrong? I assume I have messed the syntax for passing lambdas around, but cant figure out how.
Thanks.