Lesson 2 of 10

Numbers and strings

Goal

Decimal division and string methods

01

Explanation

Ruby has two main number types: Integer for whole numbers and Float for decimals. When you divide an integer by an integer, the result is an integer too, and the fraction is dropped without any warning: 7 / 2 is 3, not 3.5. Make just one side a decimal and the division keeps its fraction, as in 7.to_f / 2, or use 7.fdiv(2) directly.

A common mistake is converting to a decimal after dividing: (47 / 6).to_f gives 7.0, because the integer division happened first and the fraction was lost before the conversion. When you display the result, round(2) rounds it to two decimal places.

A string is an object with methods of its own: strip removes spaces from both ends, upcase converts letters to upper case, reverse reverses the order of the characters, split(",") breaks the string into an array at every comma, and "-" * 20 repeats the string twenty times. None of them change the original string. Each one returns a new value, which is why you can chain them one after another, like text.strip.upcase.

02

Your task

In average, work out the team's average points per game as a decimal rounded to two places. Then clean up coupon and store the result in code with no spaces and in upper case, split tags at the commas and store the result in tag_list, and build divider by repeating "=" 20 times with *.

Checks

5 checks
  1. average is a Float, not an Integer
  2. average is rounded to two places and equals 7.83
  3. code has no spaces and is in upper case
  4. tag_list is an array holding all three tags
  5. divider is made of 20 = signs
main.rb
OutputRuby 3.4 (ruby.wasm)

Loading Ruby (about 8 MB). This only happens once. Your browser keeps it after that.

–/5

Checks start once the environment is ready.

5 checks listed under “Your task”.