Created
October 22, 2012 19:36
-
-
Save havenwood/3933580 to your computer and use it in GitHub Desktop.
Perl6 Book Chapter 3 Example Ported to Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://cloud.github.com/downloads/perl6/book/2012.05.23.a4.pdf