Ruby Flashcards

1
Q

What are some basic classes?

A

Fixnum
String
Array
Hash

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

How to get user input?

A

input = gets.chomp // chomp removes \n

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

How to convert string to int and int to string?

A

string.to_i and int.to_s

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

Method and constructor format?

A
def initialize    // a constructor w/o params
  // code
end
def initialize(name, age)    // a constructor w params
  // code
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

A class with class and instance variables? Also define a sub class?

A
class MyClass
  @instanceVariable
  @@classVariable

def myMethod
localVariable = 2
end
end

class MySubClass

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

what is the spaceship operator?

A

a b

// resutls in
a  b   = 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Array Syntax?

A
animals = Array.new 
animals = ["bear", "dog", "cat"]
animals[0]  // bear
animals[0] = 1  
animals[0] // 1

arr[:slot1, :slot2, :slot3]
:slot1 // arr[0]

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

Iterate over array?

A

arr.each { |x| puts x }

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

Null in Ruby?

A

nil

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

What is a block and are they first class citizens?

A

A function w/o a name.

3.times { |x| puts x}
block-> |—————|

A block cannot be assigned to a variable as it is not a first class citizen. So one must use a proc:

b = Proc.new { |x| puts x }
b.call(x)

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

How to use yield?

A
def test
  yield 5
end
test { |x| puts x}   // called when yield is called (also works w/o param)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

A ruby while loop?

A

i = 0

while i

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

Define Ruby?

A

Object orientated
Interpreted
Dynamically typed
Strongly typed for the most part

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

How to evaluate a string before returning it?

A

One quote interprets string literraly
Double quotes evals string

x = 5
puts “Number: #{x}” // Number: 5
puts ‘Number: #{x}’ // Number: #{x}

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

Method parameter optional value with default value?

A
def myMethod(req, optional=1)
  // code
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Ruby associative array (hash)?

A
h = Hash.new
h = { "a" => 100, "b" => 200 }

or

h { :a => 100, :b => 200 } // more efficient as a new string need not be created.

17
Q

How to create setters and getters?

A

class MyClass
attr :name, :age // getters
attr_accessor :name, :age // setters

18
Q

Arrays can act as stacks?

A

a. push(3) // adds 3 to end of array

a. pop // removes first elems

19
Q

Multidimensional arrays?

A

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

a[0][0] // 1