1 year ago

#355595

test-img

mrOlympia

Ruby gem - How do I test my own ruby gem via IRB?

Okay so I am completely new to using IRB so I have no idea what I am doing wrong.

Here is my gem that I built

# frozen_string_literal: true

require_relative "badwordgem/version"
require 'yaml' #this will need to use the badlist.yml file 
module Badwordgem
  class Error < StandardError; end
  
  class Base
    class << self # assign class to itself so self does not need to be used for every method

      def badlist
        @badlist ||= YAML.load_file(File.expand_path("badlist.yml", __dir__)) # This will load in the bad words from our YML file and it will assign it to the badlist variable
      end

      def sanitize(input = "sassy") # method to take in user input to check for profanity
        word = input.downcase # It will change the user input and set it to lowercase to match our bad list of words
        badlist.each do |key, value| # For every word in the badlist assign it a key(a bad word), and a value(then replace the bad work with * symbol)
          word.gsub!(/\b#{key}\b/, value) # For each word the user has inputed replace the old word(key) with the new word(value)
        end
        word # return the word whether it contains profanity or not
      end

    end
  end


end

Essentially it tests to see if a word is bad or not based on my list of badwords.

I have installed and built it but I have no idea how to test it in IRB.

I've tried running badwordgem.sanitize(hello) etc. but that clearly is wrong as it gives me this error undefined local variable or method badwordgem' for main:Object'

What command in IRB do I type to test it??

ruby

rubygems

irb

0 Answers

Your Answer

Accepted video resources