Ruby Syntax Flashcards

1
Q

How do you make a class?

A

class Name

attr_accessor :first, :second

  def initialize(first, second)
    @first = first
    @second = second
  end

end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you make an array?

A

Dynamic Array:
array_name1 = []
array_name2 = Array.new()

Static Array:
array_name3 = [“One”, “Two”, “Three”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you write comments?

A

for single line comments

= begin

for block comments

=end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you find the number of items in an array?

A

array_name.length

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you insert a variable into a string?

A

“This is a #{quote}”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you write a while loop?

A
i = 0 (control variable)
count = 5

while i < count
DO SOMETHING
i += 1
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you write a for loop?

A

count = 5

for count in 0..4
DOES SOMETHING
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you write an until loop?

A

count = 0

begin
count += 1
end until (count==6)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you write an each loop for arrays?

A

colors = [“red”, “blue”, “green”, “yellow”]

colors.each { |x| puts x}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you write an if statement?

A
if CONDITION
  CONSEQUENT
else
  ALTERNATIVE
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you write an elsif statement?

A
if CONDITION 1
  CONSEQUENT 1
elsif CONDITION 2
  CONSEQUENT 2
else
  ALTERNATIVE
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you add items to an array?

A

array_name[n] = Value

array_name &laquo_space;Value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you write a case statement?

A

count = 10

case count
  when 1..4
    action 1
  when 5
    action 2
  else
    ALTERNATIVE
 end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you access values within a multidimensional array?

A

2d_array = [[1, 2, 3], [4, 5, 6]]

puts 2d_array[0][1]
It will print out 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you make an enumeration?

A

module Name
Thing1, Thing2, Thing3 = *1..3
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you open a file to read/write?

A

a_file = File.new(“file name”, “r”)

a_file = File.new(“file name”, “w”)

17
Q

How do you close a file?

A

a_file.close()

18
Q

How do you read from a file?

A
line_one = a_file.gets()
line_two = a_file.gets()
line_three = a_file.gets()