Closed kojix2 closed 3 years ago
Maybe there we could define a Hash constant (CANVAS_MAP
?) with mapping of Symbol to Class name and dispatch that way instead of a case? I think it would work as long as the method call is consistent for all canvases e.g.: klass.new(width, height, **kw)
And also provide a public method to (self.canvases
or self.canvas_list
) get the list of available canvasses (keys of CANVAS_MAP
), to give the Symbol list you desire.
Or did you have something else in mind?
Hi @nanobowers. Yes, you are right. That's what I was thinking about.
I'll fix it.
This issue has been resolved. Thank you for your work.
I'm not sure why to have the map at all if there is
https://stackoverflow.com/a/6084914/322020 https://docs.ruby-lang.org/en/master/ObjectSpace.html#method-c-each_object
irb> ObjectSpace.each_object(Numeric.singleton_class).to_a
=> [Complex, Rational, Bignum, Float, Fixnum, Integer, Numeric]
So:
$ irb -runicode_plot
irb> ObjectSpace.each_object(UnicodePlot::Canvas.singleton_class).to_a
=> [UnicodePlot::Canvas, UnicodePlot::DotCanvas, UnicodePlot::DensityCanvas, UnicodePlot::BrailleCanvas, UnicodePlot::BlockCanvas, UnicodePlot::AsciiCanvas, UnicodePlot::LookupCanvas]
@Nakilon Why do you thought ObjectSpace.each_object
is appropriate here? This method scans all the object slots in the heap. I don’t accept that computational complexity.
ObjectSpace.each_object
should not be used easily in the library.
@mrkn I'm not saying it should be used in a library. @kojix2 wanted to know which canvas types are available and that's how he can do that.
@Nakilon, I'm sorry for misreading. I guess @kojix2 didn't want to know there were canvas classes, but to know what canvas names were available.
@Nakilon
Thanks. ObjectSpace.each_object
is very interesting one. I did not know about this method.
I appreciate your help.
However, as @mrkn said, what I really wanted to do was to get symbols to represent the canvases and show the list of available canvases in the YouPlot command line options help.
uplot line --help
Usage: YouPlot line [options] <in.tsv>
Options for line:
--xlim FLOAT,FLOAT plotting range for the x coordinate
--ylim FLOAT,FLOAT plotting range for the y coordinate
--fmt STR xy : header is like x, y...
yx : header is like y, x...
--[no-]grid draws grid-lines at the origin
--canvas STR type of canvas
(ascii, block, braille, density, dot) <<<<<< ◆ here
Common options:
...
This is what it looks like when you actually specify the --canvas
option. This is the number of new positive cases of coronavirus in Japan, and the lineplot is displayed very nicely.
Currently it appears to be provided by the case statement. I would like to have a way to externally access the list of symbols that represent the Canvas.
https://github.com/red-data-tools/unicode_plot.rb/blob/a22f1293a63225003fabafa3acb81f45e301fed8/lib/unicode_plot/canvas.rb#L5-L18