0
0
Rubyprogramming~10 mins

File.write for writing in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to write the string 'Hello' to a file named 'greeting.txt'.

Ruby
File.[1]('greeting.txt', 'Hello')
Drag options to blanks, or click blank then click option'
Awrite
Bread
Copen
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Using File.read instead of File.write
Using File.open without a block
Trying to use File.close directly
2fill in blank
medium

Complete the code to write 'Ruby is fun!' to a file named 'fun.txt'.

Ruby
File.[1]('fun.txt', 'Ruby is fun!')
Drag options to blanks, or click blank then click option'
Aread
Bappend
Cwrite
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using File.append which adds instead of overwriting
Using File.read which only reads files
Using File.delete which removes files
3fill in blank
hard

Fix the error in the code to write 'Data' to 'data.txt'.

Ruby
File.[1]('data.txt', 'Data')
Drag options to blanks, or click blank then click option'
Aread
Bwrite
Cgets
Dputs
Attempts:
3 left
💡 Hint
Common Mistakes
Using File.read which only reads files
Using File.puts which does not exist
Using File.gets which reads input
4fill in blank
hard

Fill both blanks to write the string length of 'hello' to 'length.txt'.

Ruby
File.[1]('length.txt', 'hello'.[2].to_s)
Drag options to blanks, or click blank then click option'
Awrite
Bread
Clength
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using File.read instead of File.write
Using length instead of size (both work but only one is correct here)
Confusing read and write methods
5fill in blank
hard

Fill all three blanks to write uppercase words longer than 3 letters from the list to 'output.txt'.

Ruby
words = ['cat', 'elephant', 'dog', 'lion']\nFile.write('output.txt', words.select { |word| word.[2] [3] 3 }.map { |word| word.[1] }.join("\n"))
Drag options to blanks, or click blank then click option'
Aupcase
Blength
C>
Ddowncase
Attempts:
3 left
💡 Hint
Common Mistakes
Using downcase instead of upcase
Using < instead of > for length comparison
Using read instead of write for file output