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”
/ foward slash or backwards
foward
\ foward slash or backwards
backwards
regular expression
starts with /, end with /.
^ anchor expression
start from the beginning of any lines within the string
.. regular expression
any two characters
“xyz”.scan(/./) { |letter| puts letter {
x
y
z
“This is a test”.scan(/\w\w/) { |x| puts x }
Th is is te st
\w
any alphanumeric character or underscore (letter, digits, or underscore)
$
anchor at the end of a line
\d
any digit
\s
whitespace(spaces, tabs, newlines, and so on)
“The car costs $1000 and the cat costs $10”.scan(/\d+/) do |x|
puts x
end
d+ matches as many digits in a row
\S
any non-whitespace (any visible character)
“This is a test”.scan(/[aeiou]/) { |x| puts x }
i
i
a
e
“This is a test”.scan(/[a-m]/) { |x| puts x }
h i i a e
character classes
allows you to match against a specific set of characters
puts “String has vowels” if “This is a test” =~ /[aeiou]/
=~ matching operator
puts “String contains no digits” unless “This is a test” =~ /[0-9]/
puts String unless someString contains a digit
puts “String has vowels” if “This is a test”.match(/[aeiou]/)
returns
x = “This is a test”.match(/(\w+) (\w+)/)
puts x[0]
puts x[1]
puts x[2]
This is
This
is
x[0] contains the data matched by the entire regular expression. x[1,2] match each match group.
x=[]
x «_space;“word”
x.push(“word”)
adding elements to an empty array
x.pop
pop element from an array
x = [“hello”, “world”]
x.join
Helloworld
x = [“Word”, “Play”, “Fun”]
puts x.join(‘, ‘)
word, play, fun
puts “This is a test”.scan(/\w/).join(‘,’)
T,h,i,s,i,s,a,t,e,s,t
puts “Short sentence. Another. No more.”.split(/./).inspect
[“Short sentence”, “ Another”, “ No more”]
puts “Words with lots of spaces”.split(/\s+/).inspect
[“Words”, “with”, “lots”, “of”, “spaces”]
[1, “test”, 2, 3, 4].each { |element| puts element.to_s + “X” }
1X testX 2X 3X 4X
[1, 2, 3, 4].collect { |element| element * 2 }
[2, 4, 6, 8]. convert array on the fly
a = [1, "test", 2, 3, 4] i = 0 while (i < a.length) puts a[i].to_s + "X" i += 1 end
1X testX 2X 3X 4X
x = [1, 2, 3]
y = [“a”, “b”, “c”]
z = x + y
p z
[1, 2, 3, “a”, “b”, “c”]
x = []
puts “x is empty” if x.empty?
x is empty
x = [1, 2, 3, 4, 5]
y = [1, 2, 3]
z = x - y
p z
[4, 5]
x = [1, 2, 3]
p x.include?(“x”)
p x.include?(3)
false
true
x = [1, 2, 3]
puts x.first
puts x.last
1
3
x = [1, 2, 3]
puts x.first(2).join(“-“)
1-2
x = [1, 2, 3]
p x.reverse
[3, 2, 1]
Hashes
defined as key value pairs
dictionary = { ‘cat’ => ‘feline animal’, ‘dog’ => ‘canine animal’ }.size
keys point to the values of the dictionary declaration
size : 2
x = { “a” => 1, “b” => 2 }
x.each { |key, value| puts “#{key} equals #{value}” }
a equals 1
b equals 2
x = { “a” => 1, “b” => 2, “c” => 3 }
p x.keys
[“a”, “b”, “c”]
x = { “a” => 1, “b” => 2 }
x.delete(“a”)
p x
{“b” => 2}
x = { “a” => 100, “b” => 20 }
x.delete_if { |key, value| value < 25 }
p x
{“a”=>100}
age = 10
unless age >= 18
puts “You’re too young to use this system”
puts “So we’re going to exit your program now”
exit
end
prints out the text
age = 10
type = age < 18 ? “child” : “adult”
puts “You are a “ + type
? :
ternary operator
ternary operator makes it possible for an expression to contain a mini if/else statement.
age = 10
puts “You are a “ + (age < 18 ? “child” : “adult”)
You are a child
fruit = "orange" if fruit == "orange" color = "orange" elsif fruit == "apple" color = "green" elsif fruit == "banana" color = "yellow" else color = "unknown" end
color = “orange”
fruit = "orange" case fruit when "orange" color = "orange" when "apple" color = "green" when "banana" color = "yellow" else color = "unknown" end
color = “orange”
fruit = "orange" color = case fruit when "orange" "orange" when "apple" "green" when "banana" "yellow" else "unknown" end
“orange”
i = 1
i = i * 2 until i > 1000
puts i
1024
x = [1, 2, 3]
x.each do |y|
puts y
end
1
2
3
Code Block
anonymous, nameless method or function. passed to the method, which then runs the code block for each element.
def each_vowel(&code_block) %w{a e i o u}.each { |vowel| code_block.call(vowel) } end each_vowel { |vowel| puts vowel }
a e i o u
code_block.call(vowel)
call method on code_block to execute the code blockfor each vowel passing in the vowel variable as a parameter each time
def each_vowel %w{a e i o u}.each { |vowel| yield vowel } end each_vowel { |vowel| puts vowel }
a e i o u
yield
yield method which automatically detects any passed code block and passes control to it
print_parameter_to_screen = lambda { |x| puts x }
print_parameter_to_screen.call(100
store code block within variables
prints 100
(‘A’..’Z’).to_a.each { |letter| print letter }
ABCDEF….XYZ
(‘A’..’Z’).include?(‘r’)
false
symbols
dont contain values or objects like variables do. instead, used to maintain a consistent reference within code.
Range
The representation for an entire range of values between a start point and an
endpoint.
Regular expression
A way to describe patterns in text that can be matched and
compared against.