Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created October 22, 2012 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/3933580 to your computer and use it in GitHub Desktop.
Save havenwood/3933580 to your computer and use it in GitHub Desktop.
Perl6 Book Chapter 3 Example Ported to Ruby
use v6;
my @scores = 'Ana' => 8, 'Dave' => 6, 'Charlie' => 4, 'Beth' => 4;
my $screen-width = 30;
my $label-area-width = 1 + [max] @scores».key».chars;
my $max-score = [max] @scores».value;
my $unit = ($screen-width - $label-area-width) / $max-score;
my $format = '%- ' ~ $label-area-width ~ "s%snn"; # OMGWTFBBQ?
for @scores {
printf $format, .key, 'X' x ($unit * .value
}
scores = { 'Ana' => 8, 'Dave' => 6, 'Charlie' => 4, 'Beth' => 4 }
screen_width = 30
label_area_width = 1 + scores.keys.max_by(&:size).size
max_score = scores.values.max
unit = (screen_width - label_area_width) / max_score
scores.each do |k, v|
print "#{k}#{' ' * (label_area_width - k.size)}#{'X' * v * unit}\n"
end
@havenwood
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment