Functionality provided by the language
There is certain functionality that is required in almost every codebase. It makes sense for the language to provide this functionality, so developers don't have to reimplement it in every program they write. This set of functionality that ships with the language is referred to as the standard library. In general, a library in programming is a collection functionality that can be re-used between multiple projects. The standard library is one such example, with the one difference that it is included with the language.
You have already been introduced to a small range of functions from the standard library: print, request_number, request_boolean, request_string. In this chapter, we are going to introduce you to further functions, methods, and types provided to you by Rouge's standard library. Like with the previously introduced functions, you can directly use the functions or types introduced in this chapter and don't have to somehow include them first.
Strings
Previously we learned how to create strings and how to embed other values in them. However, the type String provides various methods, extending its functionality. A selection of the most important methods is featured in the following. A complete list of all methods on the String type can be found in the reference. The reference chapter is a detailed list of all functions and types provided by the standard library.
lengthCount the characters in the string.
Parameters:
this:
String
Return type:
NumberExample:
"Hello, World!".length()Output:
13concatenateCombine two strings to form a new one.
Parameters:
this:
String
other:
String
Return type:
StringExample:
"a".concat("b") // equivalent to "${"a"}${"b"}"to_upper_caseCapitalize every letter in the string.
Parameters:
this:
String
Return type:
StringExample:
"Hello, World!".to_upper_case()Output:
HELLO, WORLD!to_lower_caseTransform every letter in the string to lowercase.
Parameters:
this:
String
Return type:
StringExample:
"Hello, World!".to_lower_case()Output:
hello, world!containsReturns
truein case the string contains a value.Parameters:
this:
String
value:
String
Return type:
BooleanExample:
"Hello, World!".contains("Hello") "Hello, World!".contains("b")Output:
true falsereplaceReplaces every occurrence of a specified value with another.
Parameters:
this:
String
previous_value:
String
new_value:
String
Return type:
StringExample:
"Hello, World!".replace("World", "Steve")Output:
Hello, Steve!
Numbers
Numbers also feature several methods, some of which are listed below. A complete account of all methods can also be found in the reference.
round_upRound the number up to the next integer, in case it has any decimal places.
Parameters:
this:
Number
Return type:
NumberExample:
4.26.round_up() 4.round_up()Output:
5 4round_downRound the number down to the next integer, in case it has any decimal places.
Parameters:
this:
Number
Return type:
NumberExample:
4.26.round_down() 4.round_down()Output:
4 4is_integerReturns
trueif the number is an integer (whole number).Parameters:
this:
Number
Return type:
NumberExample:
4.26.is_integer() 4.is_integer()Output:
false true
Input/Output
You have already been introduced to multiple functions for requesting input from the user (request_number, request_boolean, request_string) as well as print for writing output to the screen. The standard library provides further functions for doing so, a selection of which is listed below.
print_inlineWrites a string to the screen but does not add a line break at the end.
Parameters:
value:
String
Example:
print_inline("Hello, ") print("World!")Output:
Hello, World!read_lineRequests a
Stringfrom the user, however, without a prompt.The string ends when the user hits return.
Otherwise it works just like
request_string.Parameters: None
Return type:
StringExample:
read_line()Input:
Hello WorldOutput:
Helloread_characterRequests a single character in the form of a
Stringfrom the user without a prompt.The user does not have to hit return, the character is captured directly when it is entered.
Parameters: None
Return type:
StringExample:
read_character()Input:
HelloOutput:
H