Ruby Basics Flashcards

1
Q

What Regexp character is used in ranges to match any character NOT included in the range?

A

It’s ^
ex :
[^abc] for any single character except: a, b, or c.

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

What does CSV mean and how is a .csv file structured?

A
CSV means Comma Separated Values
-------------------------------------
It looks like this : 
"Name", "Age"
"Marion", "26"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Do you know how to split a String sentence into an Array of words?

A

By using .split on the String
ex:
“All you need”.split #=> [“All”, “you”, “need”]
or
“Marion”.split(“”) #=> [“M”, “a”, “r”, “i”, “o”, “n”]

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

How do you define ranges in a Regexp?

A
By using brackets : 
-------------------------------------
/./  #=> any character
/[aB9]/  #=> a or B or 9
/[^a-c]/ #=> any character but a or b or c
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you parse a .json file?

A

require ‘json’

serialized_beers = ‘{ “beers”: [
“name”: “1664”, “appearance”: “Blond”},
“name”: “Hoegaarden”, “appearance”: “White”}
]}’

beers = JSON.parse(serialized_beers)

beers.class

=> Hash

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

What Regexp modifiers do you know?

A

/i stands for insensitive to case
/m stands for multiline
———————————-
/hello/i # will match hello, Hello, HeLlO, HELLO, …
/hello.world/m # will match hello\nworld

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

How do you define a method accepting a block with dynamic parameters?

A
You need to use yield(parameters)
-------------------------------------
def greet(name)
      capitalized_name = name.capitalize
      puts yield(capitalized_name)
end

me = “Max”

greet(me) do |name|
“Greetings, #{name}, you look quite fine today!”
end

=> “Greetings, John, you look quite fine today!”

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

What operator would you use to get the position of a Regexp pattern matched in a String?

A

“hello” =~ /l{2}/ # => 2
“hello” =~ /m{2}/ # => nil

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

What Regexp anchor would you use to specify that the pattern you look for this at the beginning of a string?

A

/\Aabc/ # any abc pattern found at the beginning of a string

It yet won’t match an abc pattern found at the beginning of a line but in one paragraph string (more selective than ^)

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

How do you store data in a .json file?

A

require ‘json’

serialized_beers = '{ "beers": [
   {
"name": "1664", "appearance": "Blond"
  },
  {
"name": "Hoegaarden", "appearance": "White"
  }
]}'

File.open(filepath, “w”) do |file|
file.write(JSON.generate(beers))
end

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

How can you get all the values of a Hash in an array?

A
You can call values on it
------------------------------------
max = { 
    first_name: "Max",
    github_nickname: "maxlm"
}

max.values

=> [“Max”, “maxlm”]

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

Which ruby module included in Array is also included in Hash?

A

Enumerable, which means you can use similar methods on Hash instance as well.

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

Do you know how to use match method with Regexp?

A

match_data = “John Doe”.match(/^(\w+) ((\w+))$/)

=>

match_data[1] # John
match_data[1] # Doe

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

Is there a way to have the index and the element when you iterate through an Array?

A

You can use each_with_index

musicians. each_with_index do |musician, index| …

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

What does XML mean and how is a .xml file structured?

A

XML means eXtensible Markup Language, starts with XML tag, contains elements and nested sub-documents, elements composed of a tag and a content.

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