Topics:
[Ruby docs Strings] (https://ruby-doc.org/core-2.4.1/String.html)
Double quote strings
"Hello World"Single quote strings string
'Hello World' puts creates a new line e.g.
print does not e.g
Concatating strings
"String" + "String"
"Hello" + "World"
= "HelloWorld"Interpolation is used to incoporate a variable into a string using #{Variable}
food = Burger
Puts "I would love to eat a #{food} right now!"
=> I would love to eat a Burger right now!NOTE Interpolation does not work in strings that have single qoutes eg.
Tips:
-
Use \ character to show ignore characters e.g.
\t t' \\ -
List of string methods
- .reverse
- .upcase
- .downcase
- .capitalize
- .to_i
[Ruby Docs Integers] (http://ruby-doc.org/core-2.4.1/Integer.html)
- Variable
result = 5 + 5+= Adds "" increments to a variable e.g.
*= Multiplies "" increments to a variable e.g.
Tips:
- When
putsis used to show a integer or float, theputsmethod will show it as a string and needs to be converted to an integer to use operators on e.g.
puts x
=> "2"
x = 2
puts x.to_i
=> 2
-puts coerces everything into strings
- List of String methods:
- .to_s (To string)
- .to_f (To float)
- .to_i (To Integer)
[Ruby Docs Floats] (https://ruby-doc.org/core-2.4.1/Float.html)
Tips:
-
When a
floatis used in an equation the result
Tips:
- All values default to
true. e.g.
hello = true
=> true
- The values of
nilandfalsedefault tofalse.
hello = false
=> false
hello = false
=> false
!hello
=> true
!!hello
=>false
hello = !hello
=> true
[Ruby Docs Integers] (https://ruby-doc.org/core-2.4.1/doc/syntax/control_expressions_rdoc.html)
If statements take booleans
steve = "paid"
if steve == "paid"
puts "drinks for everyone"
else
puts "ha"
elsif steve == "paid more money today".
puts "cry with joy"
endwhile continues until
until continues until there is a false result
running = true
car_choices = []
while doing_its_thing
puts """ Enter in your choice (pick a number)
1. Ford Falcon
2. Holden Commodore
3. Skyline
4. Telsa Model S
response = gets.chomp.to_i
puts "your response was #{response}"
if car_choices.includes? responce
case response
when 1
car_choices.push "Ford Falcon"
when 2
car_choices.push "Ford Falcon"
when 3
car_choices.push "Skyline"
when 4
car_choices.push "Tesla Model S"
end
car_choices.each do|car_choice|
puts car_choices
end
if car_choices.length == 3
break
end
car_choices.push response
doing_its_thing = false
end== does it equal (is it the same) != does not equal
- greater than or equal to
10 >= 10
=> true&&
10 >= 10 && 9 < 10
=> trueVariables are better to use for multiple responces.
array = []
or array %w{} best to use the other and is hard to distinguish what it is
- methods
| && | True | False |
|------|-------|-------|
| True | True | False |
| False| False | False |
| Or | True | False |
|------|:-----:|-------|
| True | True | True |
| False| True | False |
| Term | Definition |
|---|---|
| Constant | A variable that doesn't change. |
| Ternery Operator | expects a true or false, question mark followed by a true statement - colon - and a flase statement. |