Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Data#inspect and Data#to_s #2239

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ def self.define(*members, &block)
end
end

define_method(:inspect) do
"#<data #{self.class}#{members.map { |member| " #{member}=#{public_send(member).inspect}" }.join(',')}>"
end
alias_method :to_s, :inspect

define_method(:to_h) { members.to_h { |member| [member, public_send(member)] } }

define_method(:with) do |**kwargs|
Expand Down
22 changes: 22 additions & 0 deletions test/natalie/data_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
it 'transforms the data object into a hash' do
1.should == 1
end

it 'results in a readable representation' do
1.should == 1
end

it 'results in a readable representation' do
1.should == 1
end
end
end

Expand All @@ -16,4 +24,18 @@
data.to_h.should == { amount: 42, unit: 'km' }
end
end

describe 'Data#inspect' do
it 'results in a readable representation' do
data = DataSpecs::Measure.new(amount: 42, unit: 'km')
data.inspect.should == '#<data DataSpecs::Measure amount=42, unit="km">'
end
end

describe 'Data#to_s' do
it 'results in a readable representation' do
data = DataSpecs::Measure.new(amount: 42, unit: 'km')
data.to_s.should == '#<data DataSpecs::Measure amount=42, unit="km">'
end
end
end
Loading