Ruby运行函数调用文件中的每个项
我目前正在玩ruby,并提出了以下建议Ruby运行函数调用文件中的每个项,ruby,string,Ruby,String,我目前正在玩ruby,并提出了以下建议 require 'simple_youtube' def video_search(term, maxresults, file) video_search = Youtube::Video.find(:params => {:q => "#{term}", :"max-results" => "#{maxresults}", :v => '2'}) min = 0
require 'simple_youtube'
def video_search(term, maxresults, file)
video_search = Youtube::Video.find(:params => {:q => "#{term}", :"max-results" => "#{maxresults}", :v => '2'})
min = 0 # => setting min variable = 0
max = video_search.entry.size # => setting max to #{maxresults}
while min < max do
export_results(file,video_search.entry[min].link[0].href) # => outputs each of the results to the listed file
min +=1
end
run = exec("~/Scripts/BASH/./music.sh #{file}") #=> Automatically downloads the items
end
def export_results(file, item)
open(file, 'a') do |f|
f.puts "#{item}\n"
end
end
video_search(ARGV[0],ARGV[1],ARGV[2]) # => Call the search with arguments
# => ARGV[0] - #{term}
# => ARGV[1] - #{maxresults}
# => ARGV[2] - #{file}
Trapt Trapt
Godsmack Shine Down
etc
上面的每一行我都能读到,只是不知道如何正确地结合这两行
/users/Ondrovic/Desktop/music.txt将包含以下内容
require 'simple_youtube'
def video_search(term, maxresults, file)
video_search = Youtube::Video.find(:params => {:q => "#{term}", :"max-results" => "#{maxresults}", :v => '2'})
min = 0 # => setting min variable = 0
max = video_search.entry.size # => setting max to #{maxresults}
while min < max do
export_results(file,video_search.entry[min].link[0].href) # => outputs each of the results to the listed file
min +=1
end
run = exec("~/Scripts/BASH/./music.sh #{file}") #=> Automatically downloads the items
end
def export_results(file, item)
open(file, 'a') do |f|
f.puts "#{item}\n"
end
end
video_search(ARGV[0],ARGV[1],ARGV[2]) # => Call the search with arguments
# => ARGV[0] - #{term}
# => ARGV[1] - #{maxresults}
# => ARGV[2] - #{file}
Trapt Trapt
Godsmack Shine Down
etc
然后从文件中读取它们并运行搜索。注意:这不是一个最佳实践,因为Ruby完全是关于OO编程的 但是,以下是如何缝合代码以使其正常工作:
require 'simple_youtube'
def video_search(term, maxresults, file)
video_search = Youtube::Video.find(:params => {:q => "#{term}", :"max-results" => "#{maxresults}", :v => '2'})
min = 0 # => setting min variable = 0
max = video_search.entry.size # => setting max to #{maxresults}
while min < max do
export_results(file,video_search.entry[min].link[0].href) # => outputs each of the results to the listed file
min +=1
end
run = exec("~/Scripts/BASH/./music.sh #{file}") #=> Automatically downloads the items
end
def export_results(file, item)
open(file, 'a') do |f|
f.puts "#{item}\n"
end
end
# => Call the search with arguments
# => line - term
# => ARGV[0] - maxresults
# => ARGV[1] - file
file = '/users/Ondrovic/Desktop/music.txt' # or your can use `ARGV[0]` for term's file path
maxresults = ARGV[0] || 10
if ARGV[1]
f = File.open(file, "r")
f.each_line { |line| video_search(line, maxresults, ARGV[1]) }
f.close
else
puts 'Please provide two arguments: first as maxresults and second the file path'
end
require'simple\u youtube'
def视频搜索(术语、最大结果、文件)
video#u search=Youtube::video.find(:params=>{:q=>“#{term}”,:“max results”=>“#{maxresults}”,:v=>'2'})
最小值=0#=>设置最小变量=0
max=video_search.entry.size#=>将max设置为#{maxresults}
而最小值<最大值
导出结果(文件、视频搜索。条目[min]。链接[0]。href)#=>将每个结果输出到列出的文件
最小+=1
结束
run=exec(“~/Scripts/BASH//music.sh{file}”)#=>自动下载项目
结束
def导出结果(文件、项目)
打开(文件“a”)do | f|
f、 放置“#{item}\n”
结束
结束
#=>使用参数调用搜索
#=>行项
#=>ARGV[0]-最大结果
#=>ARGV[1]-文件
file='/users/Ondrovic/Desktop/music.txt'#或者您可以使用'ARGV[0]`作为术语的文件路径
maxresults=ARGV[0]| | 10
如果ARGV[1]
f=文件。打开(文件“r”)
f、 每行{|行|视频搜索(行,最大结果,ARGV[1]))
f、 接近
其他的
puts“请提供两个参数:第一个作为maxresults,第二个作为文件路径”
结束
你的问题是什么,你到底想做什么?@User089247想在视频搜索功能中输入一个包含搜索词的文本文件Test.txt中的数据是什么,以及/users/Ondrovic/Desktop/music.txt中的数据是什么?因此/users/Ondrovic/Desktop/music.txt中的数据就是我想要搜索的词,因此,我只需要将每个项目输入到视频搜索功能中,您应该真正修复代码缩进。最佳实践是什么?请注意,您始终可以在函数式编程风格中使用Ruby。但是由于Ruby是一种纯粹的面向对象编程语言。我鼓励您创建一个类,定义属性而不是参数,实例化类的对象,并在初始化对象和进行迭代时将这些参数作为属性传递。然而,上面的代码应该是有效的,对吗?请不要忘记接受你的问题的答案,这样如果他们解决了你的问题。祝你有一个愉快的节目。