-
Data types
- numbers (no quotes)
- boolean (no quotes)
- strings (quotes)
case-sensitive
-
Variable
- my_num = 100
- (single equal sign)
-
Six arithmetic operators
- +
- -
- *
- /
- ** - Exponentiation
- % - Modulo
-
puts
print
- put string - adds a new blank line
- print
-
editor - interpreter - console
type code in editor - interpreter reads - console shows result
-
String Methods
.length
.reverse
.upcase
.downcase
.capitalize
.include? "x"
.gsub!(/x/, "y")
.split (" ")
- "Eric".length - length of string: 4
- "Eric".reverse - "cirE"
- "Eric".upcase - "ERIC"
- "Eric".downcase - "eric"
- capitalize
- does it include x?
- global substitution
- splits at delimiter
-
Comments
#
=begin
=end
- # single-line comment
- =begin
- multi-line-
- comment
- =end
-
Naming Convention: Local Variables
-
Chained Method
- name = "Eric"
- name.downcase.reverse.upcase
-
gets.chomp
- print "What's your first name?
- "first_name = gets.chomp
- gets - gets input from user
- chomp - deletes blank line (newline) that is automatically inserted when ruby gets data
-
String Interpolation #{x}
- x = monkey
- print "Curious George is a #{x}"
-
! method
- print "What's your first name?"
- first_name = gets.chomp
- first_name.capitalize!
! changes value of variable - next time you use that variable, the !-version will be used
-
if
elsif
else
end
- if 3 < 4
- puts "3 is smaller than 4"
- elsif 3 > 4
- puts "nope"
- else puts
- "I won't get printed anyways"
- end
-
unless
else
end
- hungry = false
- unless hungry
- puts "I'm writing Ruby programs!"
- else puts "Time to eat!"
- end
- a = 4
- print "Good to Go" unless a == 5
-
Comparators (Relational Operators)
!=
==
>=
<=
<
>
-
Logical/Boolean Operators
&&
||
!
- and (true && true: true, true && anything else:false)
- or (false || false: false, false || anything else: true)
- not
-
while
end
- counter = 1
- while counter < 11
- puts counter
- counter = counter + 1
- end
-
until
- i = 0
- until i == 6
- i += 1
- end
- puts i
-
Assignment operators
+=
-=
*=
/=
- increment by
- decrease by
- multiply by
- divide by
-
for
end
- for num in 1...10
- puts num
- end
-
Inclusive and Exclusive Ranges
..
...
- .. inclusive
- ... exclusive
-
loop do
break if
end
- i = 0
- loop do
- i += 1
- print "#{i}"
- break if i > 5
- end
-
for
next if
print
end
- for i in 1..5
- next if i % 2 == 0
- print i
- end
-
array.each do |x|
x +=
print "#{x}"
end
my_array = [1, 2, 3]
- array = [1,2,3,4,5]
- array.each do |x|
- x += 10
- print "#{x}"
- end
-
.times
3.times {print "I love you"}
|
|