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 ‘-‘.