Skip to content

Commit

Permalink
Fix issue with deleting pd options from a Hash if it was the only…
Browse files Browse the repository at this point in the history
… argument breaking the guarantee that `pd` never modifies the printed object, which could cause bugs. / Fix issue with attempting to modify a frozen `Hash` when passing a frozen `Hash` as the only argument for `pd` method
  • Loading branch information
AndyObtiva committed Jun 28, 2024
1 parent beaf481 commit ec353b6
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## 1.0.1

- Fix issue with deleting `pd` options from a `Hash` if it was the only argument breaking the guarantee that `pd` never modifies the printed object, which could cause bugs.
- Fix issue with attempting to modify a frozen `Hash` when passing a frozen `Hash` as the only argument for `pd` method

## 1.0.0

- Support including class/method after file/line in every `pd` printout
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Puts Debuggerer 1.0.0
# Puts Debuggerer 1.0.1
## Debugger-less Debugging FTW
## [Featured in State of the Art Rails 2023 Edition](https://github.com/DanielVartanov/state-of-the-art-rails/tree/bd7a509f5f0ab07cebfeada779b5c73e1eaf22ed)
[![Gem Version](https://badge.fury.io/rb/puts_debuggerer.svg)](http://badge.fury.io/rb/puts_debuggerer)
Expand Down Expand Up @@ -332,7 +332,7 @@ This is the recommended way for installing in [Rails](rubyonrails.org) apps in a
Add the following to bundler's `Gemfile` (in Rails, you can optionally limit to the `:development` and `:test` groups).

```ruby
gem 'puts_debuggerer', '~> 1.0.0'
gem 'puts_debuggerer', '~> 1.0.1'
```

Run:
Expand Down Expand Up @@ -367,7 +367,7 @@ end
Or manually install and require library.

```bash
gem install puts_debuggerer -v1.0.0
gem install puts_debuggerer -v1.0.1
```

```ruby
Expand Down
3 changes: 1 addition & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ Here are tasks considered for future versions. Once done, they are moved to the

## Next

- Fix issue with attempting to modify a frozen hash when passed as value for `pd` command (check if frozen)
- Enable easier setting of the `pd` log level for a Rails application than requiring a `PutsDebuggerer.printer` lambda by offering the option `PutsDebuggerer.log_level`.
- When using last arg as hash for options, leave out options that are not puts_debuggerer-specific for printing out
- Support displaying the date/time of the pd printout via an option (local and global)
- Display class#method for instance methods and class::method for class methods
Expand Down Expand Up @@ -39,3 +37,4 @@ Here are tasks considered for future versions. Once done, they are moved to the
- Highlight the pd being printed if multiple pds exist in the same line (perhaps by calling .red on its string reusing that from ap)
- Have pd support running from JAR files in JRuby
- Fix issue with using SimpleDelegator, which seems to break puts_debuggerer output for the class that inherits from it
- Enable easier setting of the `pd` log level for a Rails application than requiring a `PutsDebuggerer.printer` lambda by offering the option `PutsDebuggerer.log_level`.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0
1.0.1
4 changes: 1 addition & 3 deletions lib/puts_debuggerer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,7 @@ def determine_options(objects)
convert_options(objects.delete_at(-1))
elsif objects.size == 1 && objects.first.is_a?(Hash)
hash = objects.first
convert_options(hash.slice(*OPTIONS).tap do
hash.delete_if {|option| OPTIONS.include?(option)}
end)
convert_options(hash.slice(*OPTIONS))
end
end

Expand Down
6 changes: 3 additions & 3 deletions puts_debuggerer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
# stub: puts_debuggerer 1.0.0 ruby lib
# stub: puts_debuggerer 1.0.1 ruby lib

Gem::Specification.new do |s|
s.name = "puts_debuggerer".freeze
s.version = "1.0.0".freeze
s.version = "1.0.1".freeze

s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
s.require_paths = ["lib".freeze]
s.authors = ["Andy Maleh".freeze]
s.date = "2024-06-04"
s.date = "2024-06-28"
s.description = "Debuggers are great! They help us troubleshoot complicated programming problems by inspecting values produced by code, line by line. They are invaluable when trying to understand what is going on in a large application composed of thousands or millions of lines of code.\nIn day-to-day test-driven development and simple debugging though, a puts statement can be a lot quicker in revealing what is going on than halting execution completely just to inspect a single value or a few. This is certainly true when writing the simplest possible code that could possibly work, and running a test every few seconds or minutes. Problem is you need to locate puts statements in large output logs, know which file names, line numbers, classes, and methods contained the puts statements, find out what variable names are being printed, and see nicely formatted output. Enter puts_debuggerer. A guilt-free puts debugging Ruby gem FTW that prints file names, line numbers, class names, method names, and code statements; and formats output nicely courtesy of awesome_print.\nPartially inspired by this blog post: https://tenderlovemaking.com/2016/02/05/i-am-a-puts-debuggerer.html (Credit to Tenderlove.)\n".freeze
s.email = "[email protected]".freeze
s.extra_rdoc_files = [
Expand Down
16 changes: 8 additions & 8 deletions spec/lib/puts_debuggerer__with_piecemeal_options__spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
it 'supports enabling header per single puts' do
PutsDebuggererInvoker.dynamic_nested_array(header: true) # support options alone
output = $stdout.string
expect(output).to eq("#{PutsDebuggerer::HEADER_DEFAULT}\n[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => {}\n")
expect(output).to eq("#{PutsDebuggerer::HEADER_DEFAULT}\n[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => {:header=>true}\n")
$stdout = StringIO.new
PutsDebuggererInvoker.dynamic_nested_array([1, [2, 3]], header: true)
output = $stdout.string
expect(output).to eq("#{PutsDebuggerer::HEADER_DEFAULT}\n[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => [1, [2, 3]]\n")
$stdout = StringIO.new
PutsDebuggererInvoker.dynamic_nested_array(name: 'Sean', header: true) # support hash including options
output = $stdout.string
expect(output).to eq("#{PutsDebuggerer::HEADER_DEFAULT}\n[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => {:name=>\"Sean\"}\n")
expect(output).to eq("#{PutsDebuggerer::HEADER_DEFAULT}\n[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => {:name=>\"Sean\", :header=>true}\n")
$stdout = StringIO.new
PutsDebuggererInvoker.dynamic_nested_array([1, [2, 3]])
output = $stdout.string
Expand All @@ -34,15 +34,15 @@
it 'supports enabling header per single puts using shortcut syntax' do
PutsDebuggererInvoker.dynamic_nested_array(h: :t) # support options alone
output = $stdout.string
expect(output).to eq("#{PutsDebuggerer::HEADER_DEFAULT}\n[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => {}\n")
expect(output).to eq("#{PutsDebuggerer::HEADER_DEFAULT}\n[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => {:h=>:t}\n")
$stdout = StringIO.new
PutsDebuggererInvoker.dynamic_nested_array([1, [2, 3]], h: :t)
output = $stdout.string
expect(output).to eq("#{PutsDebuggerer::HEADER_DEFAULT}\n[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => [1, [2, 3]]\n")
$stdout = StringIO.new
PutsDebuggererInvoker.dynamic_nested_array(name: 'Sean', h: :t) # support hash including options
output = $stdout.string
expect(output).to eq("#{PutsDebuggerer::HEADER_DEFAULT}\n[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => {:name=>\"Sean\"}\n")
expect(output).to eq("#{PutsDebuggerer::HEADER_DEFAULT}\n[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => {:name=>\"Sean\", :h=>:t}\n")
$stdout = StringIO.new
PutsDebuggererInvoker.dynamic_nested_array([1, [2, 3]])
output = $stdout.string
Expand All @@ -51,15 +51,15 @@
it 'supports enabling footer per single puts' do
PutsDebuggererInvoker.dynamic_nested_array(footer: true) # support options alone
output = $stdout.string
expect(output).to eq("[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => {}\n#{PutsDebuggerer::FOOTER_DEFAULT}\n")
expect(output).to eq("[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => {:footer=>true}\n#{PutsDebuggerer::FOOTER_DEFAULT}\n")
$stdout = StringIO.new
PutsDebuggererInvoker.dynamic_nested_array([1, [2, 3]], footer: true)
output = $stdout.string
expect(output).to eq("[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => [1, [2, 3]]\n#{PutsDebuggerer::FOOTER_DEFAULT}\n")
$stdout = StringIO.new
PutsDebuggererInvoker.dynamic_nested_array(name: 'Sean', footer: true) # support hash including options
output = $stdout.string
expect(output).to eq("[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => {:name=>\"Sean\"}\n#{PutsDebuggerer::FOOTER_DEFAULT}\n")
expect(output).to eq("[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => {:name=>\"Sean\", :footer=>true}\n#{PutsDebuggerer::FOOTER_DEFAULT}\n")
$stdout = StringIO.new
PutsDebuggererInvoker.dynamic_nested_array([1, [2, 3]])
output = $stdout.string
Expand All @@ -68,15 +68,15 @@
it 'supports enabling footer per single puts using shortcut syntax' do
PutsDebuggererInvoker.dynamic_nested_array(f: :t) # support options alone
output = $stdout.string
expect(output).to eq("[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => {}\n#{PutsDebuggerer::FOOTER_DEFAULT}\n")
expect(output).to eq("[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => {:f=>:t}\n#{PutsDebuggerer::FOOTER_DEFAULT}\n")
$stdout = StringIO.new
PutsDebuggererInvoker.dynamic_nested_array([1, [2, 3]], f: :t)
output = $stdout.string
expect(output).to eq("[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => [1, [2, 3]]\n#{PutsDebuggerer::FOOTER_DEFAULT}\n")
$stdout = StringIO.new
PutsDebuggererInvoker.dynamic_nested_array(name: 'Sean', f: :t) # support hash including options
output = $stdout.string
expect(output).to eq("[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => {:name=>\"Sean\"}\n#{PutsDebuggerer::FOOTER_DEFAULT}\n")
expect(output).to eq("[PD] #{puts_debuggerer_invoker_file}:26 in PutsDebuggererInvoker.dynamic_nested_array\n > pd *array_including_options\n => {:name=>\"Sean\", :f=>:t}\n#{PutsDebuggerer::FOOTER_DEFAULT}\n")
$stdout = StringIO.new
PutsDebuggererInvoker.dynamic_nested_array([1, [2, 3]])
output = $stdout.string
Expand Down
9 changes: 9 additions & 0 deletions spec/lib/puts_debuggerer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ def full_message
output = $stdout.string
expect(output).to eq("")
end

it 'prints frozen hash' do
hash = {a: 1, b: 2, c: 3, h: :t, f: :t}.freeze
result = PutsDebuggererInvoker.call_pd(hash)
# TODO it is odd we are getting the PutsDebuggerInvoker file, but the Array class. See if we should fix something here.
expect(result).to eq(hash)
output = $stdout.string
expect(output).to eq(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n[PD] /Users/andymaleh/code/puts_debuggerer/spec/support/puts_debuggerer_invoker.rb:60 in PutsDebuggererInvoker.call_pd\n > pd *args\n => {:a=>1, :b=>2, :c=>3, :h=>:t, :f=>:t}\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n")
end

context 'look into puts debuggerer blog post by tenderlove for other goodies to add'
context 'deadlock detection support'
Expand Down

0 comments on commit ec353b6

Please sign in to comment.