Skip to content

Refactor: Reduce duplicated code in measure.rb - #1250

Merged
tompng merged 1 commit into
ruby:masterfrom
GabrielLidenor:refactor-callback-message-measure
Sep 8, 2026
Merged

Refactor: Reduce duplicated code in measure.rb #1250
tompng merged 1 commit into
ruby:masterfrom
GabrielLidenor:refactor-callback-message-measure

Conversation

@GabrielLidenor

Copy link
Copy Markdown
Contributor

closes #1249

Why

puts "#{added[0]} is added." was written twice in the code

Solution

private method to encapsulate the logic and prevent duplication.

Comment thread lib/irb/command/measure.rb Outdated
when :on
added = IRB.set_measure_callback(arg)
puts "#{added[0]} is added." if added
measure_callback = IRB.set_measure_callback(arg)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is added in IRB.conf[:MEASURE_CALLBACKS], so it's an internal expression of type, callback, and measuring argument, but I'm not sure if the whole tuple is really called measure_callback.
Currently, the internal structure of set_measure_callback is only leaked to here. Passing internal structure to another method will increase the leak range.

How about something like this?

# Leave unrelated part
added  = IRB.set_measure_callback(arg)
# display_added_message(type) needs type, will print a message,
# and nil-check is done in here, the caller site, not in display method.
display_added_message(added[0]) if added

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback.

Good to know about the leak, that makes sense to me, thanks for spending time pointing that out.

I will address that now

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tompng comment addressed. Thank you once again.

puts "#{added[0]} is added." was typed twice in the code.
@GabrielLidenor
GabrielLidenor force-pushed the refactor-callback-message-measure branch from 774aff8 to d0ba6c1 Compare September 8, 2026 13:31
@tompng
tompng merged commit 577e65a into ruby:master Sep 8, 2026
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor: Reduce duplicated code in measure.rb

2 participants