Last active
March 24, 2016 21:10
-
-
Save veelenga/21e42a629ae1b39222d3 to your computer and use it in GitHub Desktop.
left-pad for Crystal
This file contains hidden or 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
class String | |
def leftpad(len : Int, char = ' ') | |
char, len = char.to_s, len - self.size | |
raise ArgumentError.new if char.size != 1 | |
len > 0 ? (char * len + self) : self | |
end | |
end | |
p "foo".leftpad(5) # => " foo" | |
p "foobar".leftpad(8) # => " foobar" | |
p "foobar".leftpad(6) # => "foobar" | |
p "1".leftpad(10, 0) # => "0000000001" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@veelenga HA!