Ruby Basics Flashcards
What Regexp character is used in ranges to match any character NOT included in the range?
It’s ^
ex :
[^abc] for any single character except: a, b, or c.
What does CSV mean and how is a .csv file structured?
CSV means Comma Separated Values ------------------------------------- It looks like this : "Name", "Age" "Marion", "26"
Do you know how to split a String sentence into an Array of words?
By using .split on the String
ex:
“All you need”.split #=> [“All”, “you”, “need”]
or
“Marion”.split(“”) #=> [“M”, “a”, “r”, “i”, “o”, “n”]
How do you define ranges in a Regexp?
By using brackets : ------------------------------------- /./ #=> any character /[aB9]/ #=> a or B or 9 /[^a-c]/ #=> any character but a or b or c
How do you parse a .json file?
require ‘json’
serialized_beers = ‘{ “beers”: [
“name”: “1664”, “appearance”: “Blond”},
“name”: “Hoegaarden”, “appearance”: “White”}
]}’
beers = JSON.parse(serialized_beers)
beers.class
=> Hash
What Regexp modifiers do you know?
/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 do you define a method accepting a block with dynamic parameters?
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!”
What operator would you use to get the position of a Regexp pattern matched in a String?
“hello” =~ /l{2}/ # => 2
“hello” =~ /m{2}/ # => nil
What Regexp anchor would you use to specify that the pattern you look for this at the beginning of a string?
/\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 do you store data in a .json file?
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 can you get all the values of a Hash in an array?
You can call values on it ------------------------------------ max = { first_name: "Max", github_nickname: "maxlm" }
max.values
=> [“Max”, “maxlm”]
Which ruby module included in Array is also included in Hash?
Enumerable, which means you can use similar methods on Hash instance as well.
Do you know how to use match method with Regexp?
match_data = “John Doe”.match(/^(\w+) ((\w+))$/)
=>
match_data[1] # John
match_data[1] # Doe
Is there a way to have the index and the element when you iterate through an Array?
You can use each_with_index
musicians. each_with_index do |musician, index| …
What does XML mean and how is a .xml file structured?
XML means eXtensible Markup Language, starts with XML tag, contains elements and nested sub-documents, elements composed of a tag and a content.