需要帮助将 Ruby 代码分解为方法

发布于 2024-11-04 20:29:54 字数 831 浏览 1 评论 0原文

我正在编写一个程序是为了更好地学习编程,我希望使用 RSpec,这样我也可以学习它。然而,事实上,该代码对 RSpec 并不是特别友好,因此我需要将其分解为方法,以便我可以对其进行测试。

我不需要任何人为我编写代码,但也许可以解释我如何分解它。我是编程新手,这种事情(将事物分解为方法)对我来说是一个非常困难的概念。

这就是我所拥有的:

  if params[:url] != ''
    url = params[:url] #line created so I can return url more easily (or, in general)
    words = params[:word].gsub("\n", ",").delete("\r").split(",") #.delete redundant?
    words.reject!(&:empty?)
    words.each(&:lstrip!)
    return "#{words}", "#{url}" #so that I can return url, not sure how to do that yet
  end

该代码是一个 SERP 检查器,它需要一个 url 和关键字并检查它们在搜索引擎中的位置。

对于 url,它只是用户希望检查的网站的 url...对于 word,这将是他们希望在 Google 中检查其网站的关键字。用户可以像这样填写输入表单:

Corn on the cob,
Fibonacci,
StackOverflow
Chat, Meta, About
Badges
Tags,,
Unanswered
Ask Question

I am writing a program to better learn to program and I wish to use RSpec so that I can learn that as well. However, as is, the code isn't particularly RSpec friendly, so I need to break it up into methods so that I can test it.

I don't need anyone to write the code for me, but perhaps explain how I can break it up. I am new to programming and this kind of thing (breaking things up into methods) is a really difficult concept for me.

Here's what I have:

  if params[:url] != ''
    url = params[:url] #line created so I can return url more easily (or, in general)
    words = params[:word].gsub("\n", ",").delete("\r").split(",") #.delete redundant?
    words.reject!(&:empty?)
    words.each(&:lstrip!)
    return "#{words}", "#{url}" #so that I can return url, not sure how to do that yet
  end

The code is a SERP checker, it takes a url and keywords and checks their location in the search engines.

For url, it'll just be the url of the website the user wishes to check... for word, it would be the keywords they wish to check their site against in Google.. a user may fill out the input form like so:

Corn on the cob,
Fibonacci,
StackOverflow
Chat, Meta, About
Badges
Tags,,
Unanswered
Ask Question

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╭⌒浅淡时光〆 2024-11-11 20:29:54

您的代码采用一个杂乱的字符串并将其转换为一个干净的数组。首先清理字符串,然后抛光数组。您可以为这些操作定义方法。

def clean_up_words(str)
 #code to clean str
  str
end

def clean_up_list(arr)
  #code to clean arr
  arr
end

dirty_list = clean_up_words( params[:word]).split(',') 
clean_list = clean_up_list( dirty_list )

Your code takes a sloppy string and turns it into a clean array. You first clean up the string, then you polish the array. You could define methods for these actions.

def clean_up_words(str)
 #code to clean str
  str
end

def clean_up_list(arr)
  #code to clean arr
  arr
end

dirty_list = clean_up_words( params[:word]).split(',') 
clean_list = clean_up_list( dirty_list )
节枝 2024-11-11 20:29:54
def foo params
  url = params[:url]
  url.empty? ? nil : [params[:word].scan(/[^\s\r,]+/), url]
end
  • 您正在分配 url = params[:url]。如果您打算这样做,则应该在引用相同内容的其他地方之前执行此操作,以减少对 param 调用 [] 的数量。
  • 对于要提取的单词有几个条件。 (a) 用“\n”、“,”、“\r”分割,(b) 单词长度不应为 0,(c) 白色字符应被去除。所有这些都可以组合为 scan(/[^\s\r,]+/)
  • 当 url 不为空时,您想返回两个变量。在这种情况下使用数组。
def foo params
  url = params[:url]
  url.empty? ? nil : [params[:word].scan(/[^\s\r,]+/), url]
end
  • You are assigning url = params[:url]. If you are going to do that, you should do it before other places where you refer to the same thing to reduce the amount of calling [] on param.
  • You have several conditions on the words to be extracted. (a) Either split by "\n", ",", "\r", (b) the word should not be of 0 length, (c) white characters should be stripped off. All of this can be put together as scan(/[^\s\r,]+/).
  • You want to return two variables when url is not empty. Use an array in that case.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文