Question: Ruby In this lab you will use an ERB template named lab4.txt.erb to display each students data as formatted text. The Ruby ERB class allows

Ruby

In this lab you will use an ERB template named lab4.txt.erb to display each students data as formatted text. The Ruby ERB class allows us to embed Ruby code into text templates either plain text or HTML much the same as you can embed executable code with .NET ASP pages and PHP.

Ruby allows us to open any class, even system classes, to modify existing, or add new methods. Heres an example that shows how to add a method to the a built-in String class.

# Open the class and add the method humanize # String#humanize converts underscores to spaces; used to make # database table names more human friendly class String def humanize # self indicates the current instance self.gsub('_', ' ') end end inhuman = 'user_name' puts "\"#{inhuman}\" becomes \"#{inhuman.humanize.upcase}\"." 
"user_name" becomes "USER NAME". 

For this lab you will add a new method named alternating_case to the Stringclass. This method will return a string in which the characters alternate between lower and upper case.

When your print the output of all the student data, use the alternating_caseString method to print each students GCOS field. See the Demo for and example of what your output should look like.

Example output

str = 'This is a string.' puts str.alternating_case #=> tHiS Is a sTrInG. 

In this lab you will create a Ruby class named Student to model the information contained in each users line in /etc/passwd. The /etc/passwd file contains basic user information and is public information on all Unix systems.

Your script will read the /etc/group file to get the user names of the students in our class. Use the CRN number for your section of the course.

User your modified String class in your lab4.rb file.

Create a class named Student. If youve completed the Week 7 Exercises you already have this class and can make a few modifications for this lab.

The Student class will have an instance variable and accessors for each of the 7 fields in each students line in /etc/passwd. The instance variables for the data will be:

@user_name

@password

@uid

@gid

@gcos_field

@directory

@shell

Create a class variable named @@count to keep a count of the number of student instances. Each time a new instance is created, increment @@count by one.

Each instance of Student will also have an instance variable named @count which will contain the value of @@count when the instance was created. Create a read accessor for @count. You do not need a write accessor.

Assign the value in the class variable @@count to each new instances @countvariable. Print @count in the first column of the listing for each student, following theexample.

Using the Student class and the information derived from the /etc/group file, create an instance for each class member. The Hills /etc/group file contains a line for each course. The group line for our course begins with a name what looks like this: cNNNN,c followed by four digits. Your script must extract this line each time it runs. Do not copy the class group line from /etc/group into your script. You must use Ruby to extract the data from the system files.

Populate each instances attributes with data derived from the students unique line in /etc/passwd. Those attributes are listed above. See the example Student class below for one way of creating new Student instances.

Create an ERB template file named lab4.txt.erb. Display each students formatted data using this ERB page.

Use the lab4.txt.erb ERB template to print each students formatted data. Look carefully at friendly_letter template for guidelines.

When printing the students GCOS data, use the alternating_case method you added to the String class. Consult the example output to see how this should look (it looks weird): Example output for Lab 4.

Capture the output of your script in a text file named lab4_output.txt. You will upload the output file for grading along with your lab4.rb file. It should contain the same information as the Lab 4 Demo Script but rendered as formatted plain text instead of HTML.

lab4.rb must read the correct line from /etc/passwd file for each student to extract the user information.

Create the Student information as formatted plain text using columns.

Print the elapsed time it takes for your script to run. Use the Time class to accomplish this. Start timing at the beginning of your script and print the elapsed time at the end of the script.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!