Beginner stuff Flashcards
x valid name?
yes
y2 valid name
yes
_x valid name?
yes
7x valid name?
nope (starts with digit)
this_is_a_test valid name?
yes
this is a test valid name?
no(not a single word)
this’is@a’test!
no (contains invalid characters: ‘@!
this-is-a-test
invalid (looks like subtraction)
unless means the opposite of if (t/f)
True
x y
returns 0 if x and y are equal, 1 if x is higher, and -1 if y is higher
- times do puts “Test” end
5. times do { puts “Test” }
Test Test Test Test Test
loop that counts from 1 up to 5
1.upto(5) {…}
loop that counts from 10 down to 1
10.downto(1) {…}
loops that counts every 5th number up to 100
0.step(100, 5) {…}
1.upto(5) { |number puts number } // what is |number| mean?
allows you to get hold of the number being iterated. The number being iterated is being passed down to the variable “number”.
10 / 3 = ?
3
10.0 / 3 = ?
3.333…
x.to_f
converts x to floating point
Pi = 3.141592
constant that shouldnt me changed, but can
x = %q{…}
using quotation marks is only viable for a single line, but if you want to span multiple lines, you can use this.
x = «YOLO hi YOLO
«_space;marks the start of the string literal and is followed by a delimeter of your choice (YOLO) to end the block of code
“abc” * 3
abcabcabc
puts “x” > “y”
false (ruby compares the numbers that represent the characters in the string)
puts “y” > “x”
true (ruby compares the numbers that represent the characters in the string)
?a, ?A, ?x (120), 120.chr
number, number, 120, x
x= 10, y = 20
puts “#{x} + #{y} = #{x+y}”
10+20=30
interpolation
process of inserting the expressions into a string literal.
puts “its a #{“bad “ * 3} idea
its a bad bad bad idea
concatenation
joining strings together
puts x.to_s + “ + “ + y.to_s (x=10, y=10)
10 + 10 (string literal)
concatenate “test” and “test”
“test” + “test” (testtest)
capitalize “test”
“test”.capitalize
down case “TEST”
“TEST”.downcase
“test”, take off the last index
“test”.chop
“test” reverse
“test”.reverse
“Test”.swapcase
“tEST”
“test”.length
4
puts “foobar”.sub(‘bar, ‘foo’)
“foofoo”
puts “this is a test”.gsub(‘i’, ‘’)
ths s a test
x = “This is a test”
puts x.sub(/..$/, ‘Hello’)
“This is a teHello”
“This is a test”.sub(/^../, ‘Hello’)
“Hellois a test”