Ruby gem - 如何通过 IRB 测试我自己的 ruby​​ gem?

发布于 2025-01-17 23:14:38 字数 1422 浏览 1 评论 0原文

好的,所以我是使用IRB的新手,所以我不知道自己在做什么错。

这是我

# 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

基本上构建的宝石测试,以查看一个单词是否基于我的坏字列表。

我已经安装并构建了它,但我不知道如何在IRB中进行测试。

我已经尝试过运行badwordgem.sanitize(hello)等。但这显然是错误的,因为它给了我这个错误未定义的本地变量或方法badwordgem'for main:object'

我在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??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文