Save 36% for Black Friday! Learn more

Ask A Question

Notifications

You’re not receiving notifications from this thread.

I am confused about do blocks

William Jones asked in Ruby

I am familiar with do while, do until, each do, etc. However I came across the following code that is using 'do' in a way I have not seen before and I am not sure what it is 'doing'. For example

'bounding_box [bounds.left, cursor], width: bounds.width do'

How many times would the block be executed, is there some sort of counter and how is it adjusted?

Even more confusing what is this doing,

'float do'

I have copied the complete method below if it helps. Any insights are welcome. I can't find anything with google searches that helps me understand this.

def draw_summary
bounding_box [bounds.left, cursor], width: bounds.width do
text "#{@service.scheduled_assessments.count} assessments are scheduled for next week."
move_down 10

  text 'Usage'.upcase, style: :bold
  move_down 5

  image File.join(@output_dir, WeeklyTeamStatusReportService.usage_chart_file_name),
        fit: [bounds.width, WeeklyTeamStatusReportService::USAGE_CHART_HEIGHT], position: :center
  move_down 10

  text 'Fuel'.upcase, style: :bold
  move_down 5

  image File.join(@output_dir, WeeklyTeamStatusReportService.mes_chart_file_name),
        fit: [bounds.width, WeeklyTeamStatusReportService::MES_CHART_HEIGHT], position: :center
  move_down 10

  text 'Body Comp'.upcase, style: :bold
  move_down 5

  image File.join(@output_dir, WeeklyTeamStatusReportService.body_fat_status_chart_file_name),
        fit: [bounds.width, WeeklyTeamStatusReportService::BODY_FAT_STATUS_CHART_HEIGHT], position: :center
  move_down 20

  if @service.call_out_athletes.count > 0
    text "#{@team.orgtype.t('athlete', count: @service.call_out_athletes.count, plural: true)} to watch (included on the following pages):"
    move_down 5
    @service.call_out_athletes.first(8).each_slice(3).with_index.each do |athletes, slice_idx|
      bounding_box([bounds.left + (bounds.width / 3) * slice_idx, cursor], width: bounds.width / 3) do
        float do
          athletes.each.with_index do |athlete, athletes_idx|
            text "#{athlete.name}", size: 10
            move_down 5
            if slice_idx == 2 && athletes_idx == athletes.length - 1 && @service.call_out_athletes.count > 8
              text "+ #{@service.call_out_athletes.count - 8} more", size: 10
            end
          end
        end
      end
    end
  else
    text "Great news! None of the #{@team.orgtype.t('athlete', plural: true)} scanned this period have low muscle energy levels."
  end
end

end

Reply

This is all to do with Prawn so I have my answer.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,110+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.