Skip to content

Commit 91913c7

Browse files
committed
Add numbers chapter
1 parent a34e069 commit 91913c7

9 files changed

Lines changed: 100 additions & 0 deletions

File tree

2_Numbers/1_Types_of_numbers/solution_spec.rb

Whitespace-only changes.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Types of numbers
2+
3+
---
4+
5+
*You will learn:*
6+
- which types of numbers are available in Ruby
7+
- how to create a certain type of number
8+
- different ways to create a number
9+
10+
---
11+
12+
Ruby has following types of numbers: *Integer*, *Float*, *Rational*, and *Complex*.
13+
14+
## Integers
15+
16+
An *Integer* represents a *whole number*.
17+
You can create an Integer by declaring the number, like `2000` or `-42`.
18+
Ruby allows you to write more readable numbers by using underscores, e.g. `1_000_000`.
19+
Using the underscore style does not change the value of an integer, so `2_000` is
20+
perfectly the same as `2000`.
21+
22+
Another way to create an Integer is by calling `Integer()` with a parameter, e.g.:
23+
24+
` Integer(3)` # => 3
25+
` Integer('3')` # => 3
26+
27+
Internally, Ruby uses two different classes for Integers. Dependent on how big the
28+
Integer is it uses a *Fixnum* or a *Bignum*. You don't have to worry about which class to
29+
use, Ruby is taking care of this for you!
30+
31+
However, you can check which class Ruby uses for your number by calling the `class`
32+
method on your number. Open a new terminal tab and open the interactive Ruby console
33+
with `irb`. Then check the class of different numbers:
34+
35+
` 1000.class` # => *Fixnum*
36+
` 4_611_686_018_427_387_904.class` # => *Bignum*
37+
38+
If you want to learn more, read about Fixnum and Bignum here:
39+
- http://patshaughnessy.net/2014/1/9/how-big-is-a-bignum
40+
- (ruby-doc core: Integer)
41+
42+
## Floats
43+
44+
A *Float* represents an *inexact real number*.
45+
You can create a Float by declaring a number with a decimal place, like `3.0`
46+
or `-2_000.25`.
47+
Another way to create a Float is to call `Float()` with a parameter, e.g.:
48+
49+
` Float(3)` # => 3.0
50+
` Float('3')` # => 3.0
51+
52+
Read more about Float here: (ruby-doc core: Float).
53+
54+
## Rationals
55+
56+
A *Rational* represents a rational number (a paired integer number a/b, where b > 0).
57+
You can create a Rational by calling `Rational()` with one or two parameters, e.g.:
58+
59+
` Rational(2)` # => *(2/1)*
60+
` Rational(2, 3)` # => *(2/3)*
61+
` Rarional('2/3')` # => *(2/3)*
62+
63+
Read more about Rational here: (ruby-doc core: Rational).
64+
65+
## Complex numbers
66+
67+
A *Complex* represents a complex number.
68+
You can create a complex number by calling `Complex()` with one or two parameters, e.g.:
69+
70+
` Complex(2)` # => *(2+0i)*
71+
` Complex(2.1, 0.2)` # => *(2.1+0.2i)*
72+
` Complex('2.1+0.2i')` # => *(2.1+0.2i)*
73+
` Complex('2.1@0.2')` # => *(2.0581398134666076+0.4172055946696286i)* - polar form
74+
75+
Read more about Complex here: (ruby-doc core: Complex).
76+
77+
---
78+
79+
Create some numbers of the different described types in the interactive Ruby console
80+
(`irb`) and press *v* to mark this unit green if you are done!
81+
82+
---

2_Numbers/2_Basic_operators/solution_spec.rb

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Basic operators
2+
3+
---
4+
5+
*You will learn:*
6+
- how to use basic arithmetic operators on numbers
7+
8+
---
9+
10+
You can apply

2_Numbers/3_Converting_numbers/solution_spec.rb

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Converting numbers
2+
3+
---
4+
5+
*You will learn:*
6+
- how to convert numbers to other number formats
7+
8+
---
File renamed without changes.

0 commit comments

Comments
 (0)