I'm new in ruby. I'm trying to do the following but haven't succeeded.
I've got an array of objects, let's call it objs. Each object has multiple properties, one of those is a variable that holds a number, let's call it val1. I wanna iterate through the array of objects and determine the max value of val1 through all the objects.
I've tried the following:
def init(objs)
@objs = objs
@max = 0
cal_max
end
def cal_max
@max = @objs.find { |obj| obj.val1 >= max }
# also tried
@objs.each { |obj| @max = obj.val1 if obj.val1 >= @max }
end
As I said, I'm just learning about blocks.
Any suggestion is welcome.
Thanks