Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I want that answer in ruby And also tell me how can I open file Your objective i

ID: 3701671 • Letter: I

Question

I want that answer in ruby And also tell me how can I open file Your objective in this assignment is to create a program that uses data within files to display a list of Major League baseball World Series winners and tell the user how many times a given team has won the World Series over its history. Records exist from 1903 through 2017. The data is stored in two text files that accompany the assignment. The first file, teams.txt, contains an alphabetical list of teams who have won at least once. The second file, winners.txt, contains a chronological list of the winning teams from 1903 through 2017. (The first line shows the team that won in 1903, and the last line shows the team that won in 2017. Note that the World Series was not played in 1904 and 1994.) Your program should bring up a menu with the three choices shown below: 1. Show Major League winners 2. Show number of wins for a team 3. Exit

Explanation / Answer

First i will provide you the information about file operations in Ruby

Ruby gives an entire arrangement of I/O-related techniques actualized in the Kernel module. All the I/O techniques are gotten from the class IO.

The class IO gives all the fundamental strategies, for example, read, compose, gets, puts, readline, getc, and printf.

This section will cover all the fundamental I/O capacities accessible in Ruby. For more capacities, please allude to Ruby Class IO.

The puts Statement

In the past sections, you have relegated qualities to factors and after that printed the yield utilizing puts explanation.

The puts proclamation educates the program to show the esteem put away in the variable. This will include another line toward the finish of each line it composes.

Illustration

#!/usr/container/ruby

val1 = "This is variable one"

val2 = "This is variable two"

puts val1

puts val2

This will deliver the accompanying outcome ?

This is variable one

This is variable two

The gets Statement

The gets explanation can be utilized to take any contribution from the client from standard screen called STDIN.

Example

The accompanying code demonstrates to you industry standards to utilize the gets proclamation. This code will provoke the client to enter an esteem, which will be put away in a variable val lastly will be imprinted on STDOUT.

#!/usr/receptacle/ruby

puts "Enter an esteem :"

val = gets

puts val

This will deliver the accompanying outcome ?

Enter an esteem :

This is entered esteem

This is entered esteem

The putc Statement

Not at all like the puts articulation, which yields the whole string onto the screen, the putc proclamation can be utilized to yield one character at any given moment.

Example

The yield of the accompanying code is only the character H ?

#!/usr/container/ruby

str = "Hi Ruby!"

putc str

This will deliver the accompanying outcome ?

H

The print Statement

The print explanation is like the puts proclamation. The main distinction is that the puts proclamation goes to the following line subsequent to printing the substance, though with the print articulation the cursor is situated on a similar line.

Illustration

#!/usr/receptacle/ruby

print "Hi World"

print "Hello"

This will deliver the accompanying outcome ?

Hi WorldGood Morning

Opening and Closing Files

As of recently, you have been perusing and keeping in touch with the standard info and yield. Presently, we will perceive how to play with genuine information records.

The File.new Method

You can make a File protest utilizing File.new technique for perusing, composing, or both, as per the mode string. At long last, you can utilize File.close technique to close that record.

Linguistic structure

aFile = File.new("filename", "mode")

# ... process the document

aFile.close

The File.open Method

You can utilize File.open strategy to make another document protest and relegate that record question a document. In any case, there is one contrast in the middle of File.open and File.new techniques. The distinction is that the File.open technique can be related with a square, though you can't do a similar utilizing the File.new strategy.

File.open("filename", "mode") do |aFile|

# ... process the document

end

Here is a rundown of The Different Modes of opening a File ?

Reading and Writing Files

Similar techniques that we've been utilizing for 'straightforward' I/O are accessible for all record objects. Along these lines, gets peruses a line from standard info, and aFile.gets peruses a line from the document protest aFile.

Be that as it may, I/O objects gives extra arrangement of access techniques to make our lives simpler.

The sysread Method

You can utilize the strategy sysread to peruse the substance of a record. You can open the document in any of the modes when utilizing the strategy sysread. For instance ?

Following is the info content record ?

This is a basic content document for testing reason.

Presently we should attempt to peruse this record ?

#!/usr/container/ruby

aFile = File.new("input.txt", "r")

on the off chance that aFile

content = aFile.sysread(20)

puts content

else

puts "Unfit to open record!"

end

This announcement will yield the initial 20 characters of the record. The record pointer will now be put at the 21st character in the document.

The syswrite Method

You can utilize the technique syswrite to compose the substance into a document. You have to open the document in compose mode when utilizing the technique syswrite. For instance ?

#!/usr/container/ruby

aFile = File.new("input.txt", "r+")

on the off chance that aFile

aFile.syswrite("ABCDEF")

else

puts "Unfit to open record!"

end

This announcement will express "ABCDEF" into the document.

Consider that the file possess the list of Winners fr every years like

Winners          year

England         1994

Germany       1995

USA               1996

Italy               1997 .....................

The following code will lists how many times that the teams won totally over history

Source Code

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote