Skip to content

Fix TypeError in Grid#apply_gutter when gutter is not specified#1390

Open
55728 wants to merge 1 commit intoprawnpdf:masterfrom
55728:fix-grid-gutter-typeerror
Open

Fix TypeError in Grid#apply_gutter when gutter is not specified#1390
55728 wants to merge 1 commit intoprawnpdf:masterfrom
55728:fix-grid-gutter-typeerror

Conversation

@55728
Copy link

@55728 55728 commented Mar 23, 2026

Closes #1343
Closes #1344

Problem

Since commit be66ddd ("Update code style"), define_grid raises a TypeError when called without gutter options:

define_grid(columns: 5, rows: 8)
# => TypeError: can't convert nil into Float

The same error occurs when only one of row_gutter or column_gutter is specified:

define_grid(columns: 5, rows: 8, row_gutter: 10)
# => TypeError: can't convert nil into Float (from column_gutter)

The cause is that apply_gutter was changed from options[:row_gutter].to_f (which returns 0.0 for nil) to Float(options[:row_gutter]) (which raises TypeError for nil).

Solution

Use Hash#fetch with a default value of 0:

@row_gutter = Float(options.fetch(:row_gutter, 0))
@column_gutter = Float(options.fetch(:column_gutter, 0))

This preserves the stricter Float() conversion for invalid values while defaulting to zero when options are omitted.

Tests

Added three test cases covering:

  • No gutter options specified at all
  • Only row_gutter specified (column_gutter defaults to 0)
  • Only column_gutter specified (row_gutter defaults to 0)

Note

@afdev82 previously addressed this in their fork (adnotam/prawn@327de7e) but a PR was not opened against upstream. This PR follows a similar approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Grid broken if no gutter defined TypeError: can't convert nil into Float

1 participant