Tag: ruby

如何在Ruby中修复挂起的popen3?

我使用popen3得到了意想不到的行为,我想用它来运行像ala cmd file2这样的命令。 下面的示例挂起,因此永远不会达到stdout done 。 使用除cat之外的其他工具可能会导致挂起,因此永远不会达到stdin done 。 我怀疑,我正在缓冲,但我该如何解决这个问题呢? #!/usr/bin/env ruby require ‘open3’ Open3.popen3(“cat”) do |stdin, stdout, stderr, wait_thr| stdin.puts “foobar” puts “stdin done” stdout.each_line { |line| puts line } puts “stdout done” puts wait_thr.value end puts “all done”

如何在RoR中上传文本文件并将内容解析到数据库中

到目前为止,我已设法上传文件: # In new.html.erb 并访问控制器中的文件 # In controller#create @text = params[:upload][:file] 但是,这只给出了文件名,而不是文件的内容。 我如何访问其内容? 我知道这是一个跳转,但是一旦我可以访问文件的内容,是否可以上传文件夹并遍历文件?

用Ruby语言快速排序

我试图在ruby中实现快速排序,但是在第一个pivot分区之后陷入了如何以递归方式调用的问题。 请帮助我了解如何继续,并让我知道我的编码风格到目前为止是否良好。 class QuickSort $array= Array.new() $count=0 def add(val) #adding values to sort i=0 while val != ‘000’.to_i $array[i]= val.to_i i=i+1 val = gets.to_i end end def firstsort_aka_divide(val1,val2,val3) #first partition $count = $count+1 @pivot = val1 @left = val2 @right =val3 while @left!=@right do # first divide/ partition logic if $array[@right] > $array[@pivot] then @right= […]

如何在Ruby中将ppt转换为图像?

我正在使用Ruby在网页中显示powerpoint文件的内容。 我找到了使用win32ole的解决方案,但我在linux环境中并没有用。 我认为该应用程序可以触发openoffice命令进行转换。

如何让Sinatra使用HTTPClient?

我正在尝试创建一个Facade API,通过Sinatra接收请求,然后在后端的Github API上启动HTTP请求。 在我的“你好世界”剧本中我有: #!/usr/bin/ruby require ‘httpclient’ require ‘sinatra’ get ‘/foo’ do “hello world” end 但是,它会遇到如下错误: $ ./api.rb /usr/local/share/gems/gems/sinatra-1.4.3/lib/sinatra/base.rb:1408:in `run!’: undefined method `run’ for HTTP:Module (NoMethodError) from /usr/local/share/gems/gems/sinatra-1.4.3/lib/sinatra/main.rb:25:in `block in ‘ 我不明白为什么会这样。 如果我注释掉require ‘httpclient’行,它就可以了: #!/usr/bin/ruby #require ‘httpclient’ require ‘sinatra’ get ‘/foo’ do “hello world” end $ ./api.rb [2013-06-26 21:43:12] INFO WEBrick 1.3.1 [2013-06-26 21:43:12] INFO […]

RSpec允许/期望vs期望/和_return

在RSpec中,特别是版本> = 3,之间有什么区别: 使用allow来设置返回测试双精度的参数的消息期望,然后使用expect对返回的测试双精度进行断言 只需使用expect来设置参数的期望并返回测试double 或者只是语义学? 我知道提供/指定带有expect的返回值是RSpec模拟2.13中的语法 ,但据我所知,RSpec模拟3中的语法改变为使用allow 。 但是,在下面的(传递)示例代码中,使用allow / expect或者只是expect / and_return似乎会生成相同的结果。 如果一种语法比另一种更受欢迎,也许我会期望有某种弃用通知,但由于没有,似乎两种语法都被认为是有效的: class Foo def self.bar(baz) # not important what happens to baz parameter # only important that it is passed in new end def qux # perform some action end end class SomethingThatCallsFoo def some_long_process(baz) # do some processing Foo.bar(baz).qux # […]

在数据库中上传图像

我希望将上传的图像保存在PostgreSQL数据库的bytea列中。 我正在寻找有关如何将Rails中的图像保存到bytea列的建议,最好使用示例。 我使用Rails 3.1和“pg”驱动程序连接到PostgreSQL。

如何使用Ruby和ERB(而不是Rails)的视图和布局?

如何使用Ruby和ERB(而不是Rails)的视图和布局? 今天我正在使用此代码来呈现我的视图: def render(template_path, context = self) template = File.read(template_path) ERB.new(template).result(context.get_binding) end 这非常有效,但是如何实现相同的function,但是在布局中渲染模板? 我想调用render_with_layout(template_path,context = self),以便它具有默认布局。

同一用户的Authlogic和多个会话

我正在使用Authlogic来管理我的应用程序中的会话。 但是,默认情况下,authlogic允许用户从不同的计算机多次登录。 我不希望这样(用户付费获取访问权限,我希望避免用户共享其帐户)。 查看Authlogic文档,我发现了perishable_token。 但是当试图实现它时,我只是得到一个错误,说persistence_token是必需的(当它不应该是因为我使用易腐烂的)。 你会如何使用Authlogic的function? 谢谢 :)

如何在ruby中使用javascript变量

我是rails的新手,我想用一个javascript变量到ruby嵌入代码中来在数据库中进行查询, 这是我的代码 function modify(){ var select = document.getElementById(“special_id”).value; var select2 = document.getElementById(“target_area_id”); select2.options.length = 1; select) %> select2.options[select2.options.length] = new Option(,”1″); } 我想将我的select获得的值与字段的id进行比较。 你能告诉我怎样才能在Ruby中做到这一点? 谢谢