2023-04-18 - Ruby Basics
- back to Le Wagon's Bootcamp log.
Ruby and programming basics
- data types
The challenges were cool, and the interaction with the buddies is an enriching experience.
methods I learned
# trim whitespaces from a string a_string.strip # check if a substring belongs to a string a_string.include?(a_word) # replace contents of a string initial_string.gsub(old_letter, new_letter) # get a random subset of an array an_array.sample(sample_size) # randomize(an_array) an_array.shuffle # sort an array an_array.sort
Age in days
# to use date methods we need to require the lib require "date" # create a new date (arguments are integers) new_date = Date.new(year, month, day) # today today = Date.today # number of days between two dates (endDate - startDate).to_i # read input from user (use chomp to remove trailing \n) input_string = gets.chomp # read an integer as input input_number = gets.chomp.to_i
# create a random number from between 1 and 100 random_number = rand(1..100)
# check if at least one element of # an array is present in another one array1.intersect?(array2)
IP address converter
# convert decimal to binary binary_notation = 12345.to_s(2) # convert a string with bits to its decimal value as Integer my_number = "00011001".to_i(2)