Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
io
input output
bin read
communitycreator

What is the IO.binread() method in Ruby?

okwudili onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The IO class in Ruby is used for all the input and output in Ruby. We use the binread() method to open a file, read it, and return the strings present in the file.

Syntax

IO.binread(filename)
Syntax for the binread() method of IO class

Parameter value

filename: this is the file’s name we want to open, read and return its content.

Return value

This method returns a string. This string represents the contents of the file.

Example

main.rb
test.txt
# read current file
puts IO.binread("main.rb")
puts "............"
# create a file test.txt and read
puts IO.binread("test.txt")
Using the "binread()" method of the IO class in Ruby

Explanation

  • Line 2: We read the current file, our application file main.rb, and print their contents using the binread() method.
  • Line 6: We create a file called test.txt and manually write the text Hello world! inside it. Then, we use the binread() method to read the file and print its content to the console.

RELATED TAGS

ruby
io
input output
bin read
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring