@@ -11,7 +11,10 @@ def display(obj, options = {})
1111 obj = obj . object
1212
1313 fuzzy_mime = options [ :format ] # Treated like a fuzzy mime type
14- raise 'Invalid argument :format' unless !fuzzy_mime || String === fuzzy_mime
14+ unless !fuzzy_mime || String === fuzzy_mime
15+ raise 'Invalid argument :format'
16+ end
17+
1518 if exact_mime = options [ :mime ]
1619 raise 'Invalid argument :mime' unless String === exact_mime
1720 raise 'Invalid mime type' unless exact_mime . include? ( '/' )
@@ -27,13 +30,15 @@ def display(obj, options = {})
2730
2831 # As a last resort, interpret string representation of the object
2932 # as the given mime type.
30- data [ exact_mime ] = protect ( exact_mime , obj ) if exact_mime && !data . any? { |m , _ | exact_mime == m }
33+ if exact_mime && data . none? { |m , _ | exact_mime == m }
34+ data [ exact_mime ] = protect ( exact_mime , obj )
35+ end
3136
3237 data
3338 end
3439
35- def clear_output ( wait = false )
36- IRuby ::Kernel . instance . session . send ( :publish , :clear_output , { wait : wait } )
40+ def clear_output ( wait = false )
41+ IRuby ::Kernel . instance . session . send ( :publish , :clear_output , wait : wait )
3742 end
3843
3944 private
@@ -44,15 +49,19 @@ def protect(mime, data)
4449
4550 def render ( data , obj , exact_mime , fuzzy_mime )
4651 # Filter matching renderer by object type
47- renderer = Registry . renderer . select { |r | r . match? ( obj ) }
52+ renderer = Registry . renderer . select { |r | r . match? ( obj ) }
4853
4954 matching_renderer = nil
5055
5156 # Find exactly matching display by exact_mime
52- matching_renderer = renderer . find { |r | exact_mime == r . mime } if exact_mime
57+ if exact_mime
58+ matching_renderer = renderer . find { |r | exact_mime == r . mime }
59+ end
5360
5461 # Find fuzzy matching display by fuzzy_mime
55- matching_renderer ||= renderer . find { |r | r . mime && r . mime . include? ( fuzzy_mime ) } if fuzzy_mime
62+ if fuzzy_mime
63+ matching_renderer ||= renderer . find { |r | r . mime &.include? ( fuzzy_mime ) }
64+ end
5665
5766 renderer . unshift matching_renderer if matching_renderer
5867
@@ -73,7 +82,8 @@ class Representation
7382 attr_reader :object , :options
7483
7584 def initialize ( object , options )
76- @object , @options = object , options
85+ @object = object
86+ @options = options
7787 end
7888
7989 class << self
@@ -94,7 +104,10 @@ class Renderer
94104 attr_reader :match , :mime , :priority
95105
96106 def initialize ( match , mime , render , priority )
97- @match , @mime , @render , @priority = match , mime , render , priority
107+ @match = match
108+ @mime = mime
109+ @render = render
110+ @priority = priority
98111 end
99112
100113 def match? ( obj )
@@ -114,15 +127,16 @@ def renderer
114127 @renderer ||= [ ]
115128 end
116129
117- SUPPORTED_MIMES = %w(
130+ SUPPORTED_MIMES = %w[
118131 text/plain
119132 text/html
120133 text/latex
121134 application/json
122135 application/javascript
123136 image/png
124137 image/jpeg
125- image/svg+xml )
138+ image/svg+xml
139+ ]
126140
127141 def match ( &block )
128142 @match = block
@@ -131,7 +145,7 @@ def match(&block)
131145 end
132146
133147 def respond_to ( name )
134- match { |obj | obj . respond_to? ( name ) }
148+ match { |obj | obj . respond_to? ( name ) }
135149 end
136150
137151 def type ( &block )
@@ -153,7 +167,7 @@ def priority(p)
153167
154168 def format ( mime = nil , &block )
155169 renderer << Renderer . new ( @match , mime , block , @priority )
156- renderer . sort_by! { |r | -r . priority }
170+ renderer . sort_by! { |r | -r . priority }
157171
158172 # Decrease priority implicitly for all formats
159173 # which are added later for a type.
@@ -170,9 +184,7 @@ def format(mime = nil, &block)
170184 end
171185
172186 type { Numo ::NArray }
173- format 'text/plain' do |obj |
174- obj . inspect
175- end
187+ format 'text/plain' , &:inspect
176188 format 'text/latex' do |obj |
177189 obj . ndim == 2 ?
178190 LaTeX . matrix ( obj , obj . shape [ 0 ] , obj . shape [ 1 ] ) :
@@ -183,9 +195,7 @@ def format(mime = nil, &block)
183195 end
184196
185197 type { NArray }
186- format 'text/plain' do |obj |
187- obj . inspect
188- end
198+ format 'text/plain' , &:inspect
189199 format 'text/latex' do |obj |
190200 obj . dim == 2 ?
191201 LaTeX . matrix ( obj . transpose ( 1 , 0 ) , obj . shape [ 1 ] , obj . shape [ 0 ] ) :
@@ -247,37 +257,27 @@ def format(mime = nil, &block)
247257
248258 match do |obj |
249259 defined? ( Magick ::Image ) && Magick ::Image === obj ||
250- defined? ( MiniMagick ::Image ) && MiniMagick ::Image === obj
260+ defined? ( MiniMagick ::Image ) && MiniMagick ::Image === obj
251261 end
252262 format 'image' do |obj |
253263 format = obj . format || 'PNG'
254- [ format == 'PNG' ? 'image/png' : 'image/jpeg' , obj . to_blob { |i | i . format = format } ]
264+ [ format == 'PNG' ? 'image/png' : 'image/jpeg' , obj . to_blob { |i | i . format = format } ]
255265 end
256266
257267 type { Gruff ::Base }
258- format 'image/png' do |obj |
259- obj . to_blob
260- end
268+ format 'image/png' , &:to_blob
261269
262270 respond_to :to_html
263- format 'text/html' do |obj |
264- obj . to_html
265- end
271+ format 'text/html' , &:to_html
266272
267273 respond_to :to_latex
268- format 'text/latex' do |obj |
269- obj . to_latex
270- end
274+ format 'text/latex' , &:to_latex
271275
272276 respond_to :to_tex
273- format 'text/latex' do |obj |
274- obj . to_tex
275- end
277+ format 'text/latex' , &:to_tex
276278
277279 respond_to :to_javascript
278- format 'text/javascript' do |obj |
279- obj . to_javascript
280- end
280+ format 'text/javascript' , &:to_javascript
281281
282282 respond_to :to_svg
283283 format 'image/svg+xml' do |obj |
@@ -286,21 +286,17 @@ def format(mime = nil, &block)
286286 end
287287
288288 respond_to :to_iruby
289- format do |obj |
290- obj . to_iruby
291- end
289+ format ( &:to_iruby )
292290
293- match { |obj | obj . respond_to? ( :path ) && obj . method ( :path ) . arity == 0 && File . readable? ( obj . path ) }
291+ match { |obj | obj . respond_to? ( :path ) && obj . method ( :path ) . arity == 0 && File . readable? ( obj . path ) }
294292 format do |obj |
295293 mime = MimeMagic . by_path ( obj . path ) . to_s
296294 [ mime , File . read ( obj . path ) ] if SUPPORTED_MIMES . include? ( mime )
297295 end
298296
299297 type { Object }
300298 priority ( -1000 )
301- format 'text/plain' do |obj |
302- obj . inspect
303- end
299+ format 'text/plain' , &:inspect
304300 end
305301 end
306302end
0 commit comments