-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhead_first_ruby1.rb
95 lines (71 loc) · 2.64 KB
/
head_first_ruby1.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
puts "hello world"
#in irb it's exit as a prompt to leave, in bash it's control + C
#irb evaluares the expression & shows the result, marked with =>
puts "Math operation & comparisons"
#Exponent (or index, or power) says how many times to use the number in the multiplication
print 2 ** 3
# / , • , - , **
#boolean operators
# < less than
# > greater than
# == checks to see if the two values are equal
#integers
#can use underscore _ to show 1000, e.g 1_234.56
#A number is defined by a series of digits, using a dot as a decimal mark, and optinally an underscore as a thousands separator.
puts "strings"
#ruby's strings are special because even very large strings are highly efficient to
#work with (isn't true in many other languages)
puts "Hello"
puts "world"
puts "variables"
#variables- names that refer to values, unlike JS don't have to declare variables
#in ruby, instead assign to a variable with the = symbol
#always lowercase, can contain _
#once you assign varibales you can access their values whenever you need to
print small = 8
print medium = 12
print small + medium = 20
#big difference is variables don't have types in Ruby- can hold any value you want
pie = "lemon"
pie = 3.14
#can hold strings & floating-point numbers to the same variable
#the +- operator lets you add on to the existing value of a variable
number = 3
number += 1
number #will give the value of 4, same applies with strings
string = "ab"
string += "cd"
string #will = "abcd"
#always use lowercase & separate words with _ "snake case" e.g. my_rank = 1
puts "everyhing is an object!"
#an object-orientated language - means data has useful methods
#methods = fragments of code that you can exexcute on demand
#like a modern language - common for a string to be a full-fledged object
#strings have metrhods to call
"Hello".upcase #"HELLO"
"Hello".reverse #"olleH"
#everything in Ruby is an object- even a number is an object
#benefit is that even numbers have useful methods
42.even? #true
-32.abs #32
#calling a method on an object
#object calling the method on is known as the method receiver
# RECEIVERS DOT OPERATORS METHOD NAMES
#"hello" . upcase
#-32 . abs
#file . read
#Variable names can be re-used, and re-assigned.
number = 4
number = number * 3
puts number + 2
# Using variable names can be useful to break up long lines and make code more expressive and readable.
# On formatting: Note that there are spaces around the assignment operator `=` as well as the arithmetical operators `+` and `*`.
number = 2 + 3 * 4
puts number
#Data types
#Numbers
#Strings (texts)
#True, False, and Nil
#Symbols
#Arrays
#Hashes