Basics Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

2-D hash Iteration, print key and value

A
secret_identities = { "The Batman" => "Bruce Wayne",
  "Superman" => "Clark Kent",
  "Wonder Woman" => "Diana Prince",
  "Freakazoid" => "Dexter Douglas"
}

secret_identities.each { |key, value| puts “#{key}: #{value}”}

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

Declare hash literally

A

hash = { key1 => value1,
key2 => value2,
key3 => value3
}

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

Declare hash using “new” keyword

A

my_array = Hash.new

note: “Hash” must be capitalized.

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

Add element to hash declared literally

A
pets = Hash.new
pets["Donder"] = "dog"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Access value in hash using keyword

A

pets[“Stevie”]

will return “cat”

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

Iteration through array

A

languages = [“HTML”, “CSS”, “JavaScript”, “Python”, “Ruby”]

languages.each{ |x| puts “#{x}” }

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

If statement

A

if 5 == 5
print “Yes, it does”
end

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

Else

A
if 5 == 5
  print "Yes, it does"
else
  print "What a strange world."
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Elsif

A
puts "Choose a number"
number = Integer(gets.chomp)
if number == 5
  print "Yes, it does"
elsif number > 5
  print "Hmmm"
else
  print "What a strange world."
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Unless

A

hungry = false

unless hungry
  puts "I'm writing Ruby programs!"
else
  puts "Time to eat!"
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Get input from user

A

variable_name = gets.chomp

chomp eliminates newline character

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

Split method

A

puts “Please enter a string…”
text = gets.chomp
words = text.split

creates an array out of string

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

Sort_By Method

A

h = h.sort_by {|a, b| b }

# the hash h will be sorted by increasing order of b, or the value of each 
# key-value pair
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Reverse Method

A

.reverse!

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

Define a method

A

def welcome
puts “Welcome to Ruby!”
end

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

Splat argument

A
def what_up(greeting, *bros)
  bros.each { |bro| puts "#{greeting}, #{bro}!" }
end

what_up(“What up”, “Justin”, “Ben”, “Kevin Sorbo”)

17
Q

Blocks

A

1.times do
puts “I’m a code block!”
end

1.times { puts “As am I!” }

18
Q

Sorting

A

my_array.sort!

19
Q

Combined comparison operator

A
20
Q

Length of string

A

string.length

21
Q

Reverse string

A

string.reverse

22
Q

Uppercase/Lowercase

A

string. upcase

string. downcase

23
Q

Multi-line comments

A

=begin

=end

24
Q

! operator

A

Means change the value, not a copy of value

25
Q

Get input from user

A

gets

my_var = gets.chomp

26
Q

While loop

A
counter = 1
while counter < 11
  puts counter
  counter = counter + 1
end
27
Q

Until loop

A
counter = 1
until counter == 11
  puts counter
  # Add code to update 'counter' here!
  counter = counter + 1
end
28
Q
  • Splat Operator
A

May receive more than one value (as parameter)

29
Q

Code Blocks

A

blocks as methods that don’t have name

1.times do
puts “I’m a code block!”
end

1.times { puts “As am I!” }

30
Q

Parameter default to false in method definition

A
def alphabetize(arr, rev=false)
end
31
Q

Getter/Setter in one

A

attr_accessor :var1, var2, etc.