Question
Help in Ruby Programming. Create a Ruby definition which adds some small methods to the standard String and Array classes which might be of some
Help in Ruby Programming.
Create a Ruby definition which adds some small methods to the standard String and Array classes which might be of some use in writing a program to generate HTML output. The methods are:
String#hesc
Add the hesc method to the String class. It takes no arguments, and returns a copy of the string with each of the characters and & replaced with their appropriate HTML entities. For instance,
"I like less-than (
returns the string
"I like less-than (
String#tag
Add the tag method to the String class. It takes one argument, which is a string giving an HTML tag. The tag method returns a new string, constructed of the argument surrounded by angle brackets, and followed by the first word of the argument as a closing tag, leading with . Between is the original string after processing through hesc. For instance, "foo".tag("i"); returns the string foo. The purpose of the first word of the argument rule is to produce a correct closing tag when the tag includes attributes. The word is delimited by spaces. For instance,
"The Many Uses Of &".tag("h1 color=\"blue\"")
will return
The Many Uses Of &
Array#tag
This simply returns a copy of the array with tag applied to each member.
Array#nest
This takes a single argument which is a tag under the same rules as String#tag. It returns a copy of the string, with opening and closing tags added to the front and back. (The added tags are strings.)
Place your Ruby code in a file which can be brought in with load.
For instance here how the output should look like I have attached picture also and in I wrote it too both are same.
OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------
irb(main):001:0> load "htmlgen.rb"
=> true
irb(main):002:0> "a
=> "a
irb(main):003:0> "a
=> "a
irb(main):004:0> ["a","b","c","d"].tag("i")
=> ["a", "b", "c", "d"]
irb(main):005:0> ["a","b","c","d"].tag("i").nest("tr id=\"foo\"")
=> ["", "a", "b", "c", "d", ""]
irb(main):006:0> ("1".."10").to_a.tag("td").nest("tr").join
=> "12345678910"
irb(main):007:0> ["al","bill","sally","joe"].tag('div color="blue"').nest("div")
=> ["
", "
al
", "
bill
", "
sally
", "
joe
", "
"]
irb(main):008:0> ["a","b","c"].nest("a href=\"nowhere\"")
=> ["", "a", "b", "c", ""]
--------------------------------------------------------------------------------------------------------------
OUTPUT in PICTURE
irb(main):001:0> load "htmlgen.rb" => true irb(main):002:0> "a "a "a "a " irb(main):004:0> ["a", "b","c","d"].tag("i") => ["a", "", "c", "d"] irb(main):005:0> ["a", "b","c","d"].tag("i").nest("tr id=\"foo\"") => ["Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started