Skip to content

Commit 652cf88

Browse files
committed
Add pretty_print support for Ruby's pp library
Implement pretty_print methods for AI::Chat and AI::Response to provide clean output with Ruby's built-in pp command. Features: - Truncates long message content to 80 characters - Hides sensitive instance variables (@api_key, @client) - Displays objects with proper notation and indentation - Works alongside amazing_print for maximum flexibility
1 parent 4247e43 commit 652cf88

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

lib/ai/chat.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,37 @@ def inspect
181181
"#<#{self.class.name} @messages=#{messages.inspect} @model=#{@model.inspect} @schema=#{@schema.inspect} @reasoning_effort=#{@reasoning_effort.inspect}>"
182182
end
183183

184+
# Support for Ruby's pp (pretty print)
185+
def pretty_print(q)
186+
q.group(1, "#<#{self.class}", '>') do
187+
q.breakable
188+
189+
# Show messages with truncation
190+
q.text "@messages="
191+
truncated_messages = @messages.map do |msg|
192+
truncated_msg = msg.dup
193+
if msg[:content].is_a?(String) && msg[:content].length > 80
194+
truncated_msg[:content] = msg[:content][0..77] + "..."
195+
end
196+
truncated_msg
197+
end
198+
q.pp truncated_messages
199+
200+
# Show other instance variables (except sensitive ones)
201+
skip_vars = [:@messages, :@api_key, :@client]
202+
instance_variables.sort.each do |var|
203+
next if skip_vars.include?(var)
204+
value = instance_variable_get(var)
205+
unless value.nil?
206+
q.text ","
207+
q.breakable
208+
q.text "#{var}="
209+
q.pp value
210+
end
211+
end
212+
end
213+
end
214+
184215

185216
private
186217

lib/ai/response.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,16 @@ def initialize(response)
99
@total_tokens = @usage[:total_tokens]
1010
end
1111

12+
# Support for Ruby's pp (pretty print)
13+
def pretty_print(q)
14+
q.group(1, "#<#{self.class}", '>') do
15+
instance_variables.sort.each_with_index do |var, i|
16+
q.breakable
17+
q.text "#{var}="
18+
q.pp instance_variable_get(var)
19+
q.text "," if i < instance_variables.length - 1
20+
end
21+
end
22+
end
1223
end
1324
end

0 commit comments

Comments
 (0)