Ruby Le Wagon Flashcards
Commenting in Ruby?
#
Two Types of Numeric Numbers in Ruby?
Integer for whole numbers
Float for decimal numbers
How to determine the type of data in Ruby?
.class
name = “Dimitri”
puts name.class # => will return String
Checking for odd or even in Ruby?
10.even? # => will return true
22.odd? # => will return false
Changing an object in a string in Ruby?
.to_s
How to get an integer from a float in Ruby?
.round
Upper Case, capitalizing, reversing in Ruby?
.upcase, .capitalize, .reverse
How to sort arrays in Ruby?
with .sort and .reverse
my_array = [1, 3, 2, 6]
my_array.sort … [1, 2, 3, 6]
my_array.sort.reverse … [6, 3, 2, 1]
Converting Strings to Numbers in Ruby?
to_i –> string to interger
to_f –> string to float
e.g.
“5”.to_i —> 5
Shorthand in Ruby for converting a method into a block
&:
It gets passed to methods like ‘map’ or ‘select’ for concise code.
e.g.
string.map(&:to_i) –> iterates over an string and converts it into numbers(if htere are nums)
Converting Numbers to String in Ruby?
With the ‘to_s’ method.
e.g.:
number = 42
string_number = number.to_s
puts string_number # Output: “42”
How to get the highest and/or lowest numbers of an array in Ruby?
.min
.max
.minmax –> array of the highest and lowest number
How does the fetch method work?
.fetch(key, default value)
If the key exists in the dictionary, fetch returns the corresponding value.
If the key does not exist, fetch returns the default value e.g. (0).
This effectively ensures that even if the key is not found in the dictionary, the expression will not raise an error, but instead return 0.
Bash command for the filepath of the current directory?
pwd (print working directory)
Bash command that lists files and directories in current directory?
ls
Bash command that navigates from one folder to another?
cd path/to/go
Bash command that creates a new directory?
mkdir folder_name
Bash command that creates a new file?
touch file_name
Bash command that moves the file into the directory?
mv file_name path/to/directory?
Bash command that removes a file?
rm path/to/file
Bash command that display the content of a file?
cat file_name
In Ruby, how to use the scan method to extract substrings from a string based on a specified pattern and return an array of matches?
.scan(>pattern<)
How to delimit a regular expression pattern in Ruby?
With two forward dashes:
/…/
What are character classes in Ruby?
A character class in regular expressions is a set of characters enclosed within square brackets […]. It allows you to specify a group of characters that you want to match at a particular position in the string. For example:
[abc] matches any one of the characters ‘a’, ‘b’, or ‘c’.
[a-z] matches any lowercase letter from ‘a’ to ‘z’.
[0-9] matches any digit from ‘0’ to ‘9’.
[-] matches either an underscore ‘’ or a hyphen ‘-‘.
How can you concatenate the items of an array in Ruby?
.join(“…”)
… = the characters between the items
What are the exclusive and inclusiv operators in Ruby?
… is exclusiv (0…3»_space; 0, 1, 2)
.. is inclusive (0..3)» 0, 1, 2, 3)
How to split a String sentence into an Array of words in Ruby?
“string”.split(“ “)
Do you know a shortcut to define an array of strings in Ruby?
There are three ways you can define an array of strings without typing quotes
%w[Huey Dewey Louie] #=> [“Huey”, “Dewey”, “Louie”]
What is the main difference between single quotes ‘ and double quotes “ in Ruby?
You can only interpolate between double quotes:
“two: #{1 + 1}”
How can you get the position (index) of an item in an array in Ruby?
You can call .index on the array, passing it the item you look for as an argument
beatles = [“John”, “Ringo”, “Paul”, “George”]
beatles.index(“John”) #=> 0
Warning: if the array has several occurrences of the item, this will return only the index of the first one.
Do you know the best way to iterate through the items of an array in Ruby?
[1, 2, 3].each do |num|
puts num
end
[1, 2, 3].each { |num| puts num }
How can you delete an item from an array in Ruby?
.delete:
beatles = [“John”, “Ringo”, “Paul”, “George”]
beatles.delete(“John”)
beatles #=> [“Ringo”, “Paul”, “George”]
You can also call .delete_at on the array, passing it the index of item you want to delete as an argument:
beatles = [“John”, “Ringo”, “Paul”, “George”]
beatles.delete_at(0)
beatles #=> [“Ringo”, “Paul”, “George”]
What is the opposite of while in Ruby?
until:
until condition
# loops while condition
is falsy
end
How can you test if an item is included in an array in Ruby?
.include?
beatles = [“John”, “Ringo”, “Paul”, “George”]
beatles.include?(“John”) #=> true
beatles.include?(“Boris”) #=> false
Is there a way to have the index and the element when you iterate through an Array in Ruby?
You can use #each_with_index
musicians = [“Jimmy Page”, “Robert Plant”, “John Paul Jones”, “John Bonham”]
musicians.each_with_index do |musician, index|
puts “#{index + 1} - #{musician}”
end
1 - Jimmy Page
# 2 - Robert Plant
# 3 - John Paul Jones
# 4 - John Bonham
Which iterator should you call on an Array to get another Array where all the elements were subject to the same treatment in Ruby?
musicians = [“Jimmy Page”, “Robert Plant”, “John Paul Jones”, “John Bonham”]
musicians.map do |musician|
musician.upcase
end
=> [“JIMMY PAGE”, “ROBERT PLANT”, “JOHN PAUL JONES”, “JOHN BONHAM”]
How can you group Array items by pair or more in Ruby?
.each_slice(no. of items).to_a
How do you remove items matching a condition from an Array in Ruby?
.reject
musicians = [“Jimmy Page”, “Robert Plant”, “John Paul Jones”, “John Bonham”]
musicians.reject do |musician|
musician.start_with?(“J”) # reject only the elements that start with a “J”
end
=> [“Robert Plant”]
How would you sort an Array with a given sorting criteria in Ruby?
.sort_by
[“apple”, “pear”, “fig”].sort_by { |word| word.length }
#=> [“fig”, “pear”, “apple”]