-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruby-basics.rb
136 lines (103 loc) · 2.52 KB
/
ruby-basics.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
################################
print "Hello World"
#### Draw a shape #######
puts "DRAW ASHAPE"
puts " /|"
puts " / |"
puts " / |"
puts "/___|"
### VARIABLES ###
puts "VARIABLES"
character_name = "Jhon"
character_age = "15"
puts ("There once was a man named " + character_name)
puts ("he was "+ character_age + " years old.")
puts ("He really liked the name "+ character_name)
puts ("but didn't like being " + character_age)
### Data type ####
puts "DATA TYPE"
#String:
name = "Mike"
# number (as integer)
age = 75
# float
gpa = 3.2564
#boolean
ismale = true
istall = false
# null (Doesn't have a value)
flaw = nil
#### Working with Strings ####
puts "WORKING WITH STRINGS"
# print quotation mark \"
puts " \" Karol"
# print new line \n
puts " \n Karol"
#Methods
phrase = "Giraffe Academy"
#Upper Case
puts phrase.upcase()
#Lower Case
puts phrase.downcase()
#strip (Remove spaces)
phrase = " Hello "
puts phrase.strip()
#Length
puts phrase.length()
# Includes (validate if the string has the pattern)
phrase = "This is my name"
puts phrase.include? "This" #true
puts phrase.include? "age" #false
#Manage positions as an array
puts phrase[0] #first position
puts phrase[3] #fourth position
#range:
puts phrase[0,4] #from fist position to fourth position
#get index
puts phrase.index("m")
puts phrase.index("name") #give the index where starts
#Math and Numbers
puts "MATH AND NUMBERS"
puts 5
puts 5 + 9 #sum
puts 5 - 3 #substract
puts 10 / 2 #divide
puts 4 * 3 # multiply
puts 10 % 5 #mudulus
puts 2 ** 3 # exponent
puts "convert to String (.to_s)"
num = 20
puts ("my favorite number is: " + num.to_s)
num = -20
### Methods
puts num.abs() #absolute value
num = 20.7234
puts num.round() ## Rounded Up (>=5)
num = 20.123
puts num.round() ## Rouded down (<5)
num = 20.1
puts num.ceil() ## High value
num = 20.9
puts num.floor() # low value
puts "Math class"
puts Math.sqrt(16)
puts Math.log(1)
puts "With Math is posible to do Sine, cosine, tangent functions"
puts "Two basic types of Numbers (Integers and float points)"
puts "integer with integer got an Integers"
puts "float with float got a float"
puts "integer with float got a float"
puts 10 + 1
puts 10.0 + 1.0
puts 10 + 7.0
########################
# Getting user input
puts "GETTING USER INPUT"
## Install package platformio-ide-terminal
## Run ruby file $ ruby giraffe.rb
puts "Enter your name: "
name = gets #With newline
puts ("Hello " + name + ", you are cool")
puts "Enter your name again: "
name2 = gets.chomp() # No new newline
puts ("Hello " + name2 + ", you are cool")