dimanche 2 octobre 2022

how to return a value in a call back function?

I tried the following two methods. Only the method 2 can return the region value if I passed output as an argument and reassigned the output to the region variable. How can I make the method 1 returning the region value? It finds the region value but at the end, the output returns an empty array in method one. How can I make the method one return a region value instead of an empty array? method 1

class abc

get_ticket_comment(){

  region=get_region(tickets)
  
}

get_region(tickets){
  output=[]
  tickets.each do |ticket|
    if ticket.start_with? "a" and ticket.loction.nil?
      get_region(ticket)
      if !output.empty?()
        return
      end
    elsif ticket.start_with? "a"
      if ticket.location
        output<< ticket.location
        break
    end
  end
return output
}

end

method 2

class abc

get_ticket_comment(){
  output=[]
  region=get_region(tickets, output)
  region=output
}

get_region(tickets,output){
  tickets.each do |ticket|
    if ticket.start_with? "a" and ticket.loction.nil?
      get_region(ticket,output)
      if !output.empty?()
        return
      end
    elsif ticket.start_with? "a"
      if ticket.location
        output<< ticket.location
        break
    end
  end
}
end

Aucun commentaire:

Enregistrer un commentaire