Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerRank Ruby – Strings – Methods II problem solution

YASH PAL, 31 July 2024

In this HackerRank Ruby – Strings – Methods II problem solution In this challenge, your task is to write the following methods:

mask_article which appends strike tags around certain words in a text. The method takes 2 arguments: A string and an array of words. It then replaces all the instances of words in the text with the modified version.

A helper method strike, given one string, appends strike-off HTML tags around it. The strike-off HTML tag is <strike></strike>.

For example:

> strike(“Meow!”) # => “<strike>Meow!</strike>”

> strike(“Foolan Barik”) # => “<strike>Foolan Barik</strike>”

> mask_article(“Hello World! This is crap!”, [“crap”])

“Hello World! This is <strike>crap</strike>!”

Apply the helper method in completing your main method.

HackerRank Ruby - Strings - Methods II problem solution

Problem solution.

# Enter your code here
def strike word
    "<strike>#{word}</strike>"
end

def mask_article s, arr
    arr.each do | w |
        striked = strike w
        s = s.gsub( /#{Regexp.escape( w )}/, striked )
    end
    return s
end

Second solution.

def strike (s)
    "<strike>" + s + "</strike>"
end

def mask_article (str, arr)
    arr.each do |word|
        str.gsub!(word, strike(word))
    end
    str
end

Third solution.

# Enter your code here
def mask_article(article, strike_words)
    strike_words.reduce(article) { |article, word| article.gsub(word, strike(word)) }
end

def strike(string)
    "<strike>#{string}</strike>"
end

coding problems solutions Ruby Solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes