0
0
Rubyprogramming~10 mins

File.read for entire content 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 read the entire content of 'example.txt' into the variable 'content'.

Ruby
content = File.[1]('example.txt')
Drag options to blanks, or click blank then click option'
Aopen
Bclose
Cwrite
Dread
Attempts:
3 left
💡 Hint
Common Mistakes
Using File.open instead of File.read, which returns a file object, not the content.
Using File.write which is for writing to files, not reading.
2fill in blank
medium

Complete the code to read the entire content of a file named 'data.txt' and print it.

Ruby
puts File.[1]('data.txt')
Drag options to blanks, or click blank then click option'
Aread
Bclose
Cwrite
Dopen
Attempts:
3 left
💡 Hint
Common Mistakes
Using File.open without reading the content.
Using File.write which is for saving data, not reading.
3fill in blank
hard

Fix the error in the code to read the entire file content into 'text'.

Ruby
text = File.[1]('notes.txt')
Drag options to blanks, or click blank then click option'
Aread
Bopen
Cclose
Dwrite
Attempts:
3 left
💡 Hint
Common Mistakes
Using File.open returns a file object, not the content.
Using File.close or File.write are not for reading.
4fill in blank
hard

Fill both blanks to read the entire content of 'log.txt' and store it in 'log_data'.

Ruby
log_data = File.[1]('log.txt').[2]
Drag options to blanks, or click blank then click option'
Aread
Bopen
Cstrip
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Using File.read with close which is not a string method.
Trying to use File.close which does not return content.
5fill in blank
hard

Fill all three blanks to read the entire content of 'story.txt', convert it to uppercase, and remove trailing spaces.

Ruby
content = File.[1]('story.txt').[2].[3]
Drag options to blanks, or click blank then click option'
Aread
Bupcase
Cstrip
Dopen
Attempts:
3 left
💡 Hint
Common Mistakes
Using File.open instead of File.read for reading content.
Mixing up the order of string methods.