Complete the code to open a file named 'example.txt' for reading.
file = File.[1]('example.txt')
The File.open method is used to open a file in Ruby.
Complete the code to write the string 'Hello' to a file.
File.open('greeting.txt', 'w') do |file| file.[1]('Hello') end
The write method writes a string to the file.
Fix the error in the code to read all lines from a file.
lines = File.[1]('data.txt').readlines
You must open the file before calling readlines to get all lines.
Fill both blanks to create a hash with words as keys and their lengths as values, only for words longer than 3 characters.
lengths = {word: word.[1] for word in words if word.[2] > 3}Use length to get the length of the word and > to filter words longer than 3 characters.
Fill all three blanks to create a hash with uppercase words as keys and their lengths as values, only for words longer than 4 characters.
result = {word.[1]: word.[2] for word in words if word.[3] > 4}Use upcase to convert words to uppercase, size to get length, and > to filter words longer than 4.