Dive into Ruby’s file handling, serialization using JSON and YAML, and working with CSV files. These questions will test your understanding and help you work effectively with files in Ruby.
File.read()File.open()File.write()File.open('r')File.open('file.txt', 'w')File.write('file.txt')File.open('file.txt', 'r')File.write('file.txt', 'w')File.read()File.gets()File.open()File.readlines()File.open('file.txt')File.read()File.each_line()File.gets()File.open()?r (read-only)w (write)a (append)rb (read-binary)File.open('file.txt', 'a')File.open('file.txt', 'w')File.write('file.txt', 'a')File.append('file.txt')File.append()File.write()File.open()File.write_data()File.close()close()File.open().close()endbegin and rescuetry and catchif and elseerror and exceptionrb) in Ruby?jsonyamljson_purejson_serializerJSON.to_json()JSON.dump()JSON.parse()JSON.generate()JSON.parse()JSON.to_ruby()JSON.read()JSON.load()File.write()JSON.generate()JSON.dump()JSON.create()yamljsonyaml_parserserializeYAML.load()YAML.dump()YAML.parse()YAML.stringify()YAML.parse()YAML.load()YAML.to_ruby()YAML.import()nilFile.read() and File.write()JSON.read() and JSON.write()File.json_read() and File.json_write()JSON.load() and JSON.dump()csvjsonyamlcsv_readerCSV.read()CSV.load()CSV.parse()CSV.open()CSV.write()CSV.dump()CSV.generate()CSV.open()CSV.open(file, { col_sep: ";" })CSV.read(file, { delimiter: "," })CSV.parse(file, { sep: ";" })CSV.write(file, { delimiter: "|" })CSV.each()CSV.gets()CSV.line()CSV.each_line()CSV.write(file, data)CSV.open(file, data)CSV.generate(file, data)CSV.dump(file, data)CSV.foreach() method do in Ruby?CSV.open('file.csv', encoding: 'utf-8')CSV.read('file.csv', encoding: 'utf-8')CSV.load('file.csv', encoding: 'utf-8')CSV.write('file.csv', encoding: 'utf-8')CSV.to_hash()CSV.parse_hash()CSV.read_hash()CSV.each_with_index()| Qno | Answer (Option with the text) |
|---|---|
| 1 | b) File.open() |
| 2 | a) File.open('file.txt', 'w') |
| 3 | a) File.read() |
| 4 | c) File.each_line() |
| 5 | a) r (read-only) |
| 6 | a) File.open('file.txt', 'a') |
| 7 | b) File.write() |
| 8 | a) File.close() |
| 9 | a) begin and rescue |
| 10 | a) It reads the file as a binary stream |
| 11 | a) json |
| 12 | d) JSON.generate() |
| 13 | a) JSON.parse() |
| 14 | c) JSON.dump() |
| 15 | a) yaml |
| 16 | b) YAML.dump() |
| 17 | b) YAML.load() |
| 18 | a) Arrays and Hashes |
| 19 | a) It raises an error |
| 20 | a) Using File.read() and File.write() |
| 21 | a) csv |
| 22 | a) CSV.read() |
| 23 | d) CSV.open() |
| 24 | b) CSV.read(file, { delimiter: "," }) |
| 25 | a) CSV.each() |
| 26 | a) CSV.write(file, data) |
| 27 | a) It reads the file line by line |
| 28 | a) CSV.open('file.csv', encoding: 'utf-8') |
| 29 | b) It will treat all data as a single column |
| 30 | a) CSV.to_hash() |