Tag: ruby

Ruby数组初始化

我需要知道这两个语句之间的技术差异以及它的行为方式: arr = Array.new(3, “abc”) => [“abc”,”abc”,”abc”] arr.last.upcase! => “ABC” arr =>[“ABC”,”ABC”,”ABC”] # which is **not** what I excepted 另一方面: arr = Array.new(3){“abc”} => [“abc”,”abc”,”abc”] arr.last.upcase! =>”ABC” arr => [“abc”,”abc”,”ABC”] # which is what I excepted

在Ruby中使用浏览器作为GUI

在vbscript中,通常使用浏览器(IE)作为GUI。 请参阅下面的示例,它要求输入名称并将其返回给脚本。 在Ruby中你有一些像Tcl和Shoes的GUI,但我想知道如何在浏览器中这样做。 什么是最简单的Ruby解决方案? 所以没有exta gems或package,没有已经运行的服务器..如果需要gem,最好是在Windows中运行而没有问题。 这里是vbscript示例 Set web = CreateObject(“InternetExplorer.Application”) If web Is Nothing Then msgbox(“Error while loading Internet Explorer”) Wscript.Quit Else with web .Width = 300 .Height = 175 .Offline = True .AddressBar = False .MenuBar = False .StatusBar = False .Silent = True .ToolBar = False .Navigate “about:blank” .Visible = True end […]

从ActiveRecord获得排名

如果User有积分,我如何获得用户排名,假设标准定位: require ‘active_record’ class User [, , , ] User.find_by_id(2).rank # => 1 User.find_by_id(3).rank # => 3 User.find_by_id(1).rank # => 3 我得到了明显的印象,这将是数据库密集型。 我并不太担心,但显然需要较少处理能力的解决方案才是赢家! 如果给予具有相同数量的点的两个用户相同的等级太复杂,我准备接受上面的用户1可以是等级3,而用户3可以是等级4。 编辑: 我实际上是在一个单独的课堂上分享gmy分数 – 因为每个事件都需要一定数量的分数。 class Event < ActiveRecord::Base belongs_to :user end class User ?’,points]) + 1 end end 关于如何使这更简洁的任何想法? 提前致谢

Heroku Rails 4无法连接到服务器:连接被拒绝

使用postgres。 一直无法推动。 没有任何运气试过这个: config.assets.initialize_on_precompile = false —–>为Rails资产管道准备应用程序 Running: rake assets:precompile rake aborted! could not connect to server: Connection refused Is the server running on host “127.0.0.1” and accepting TCP/IP connections on port 5432?

快速获取远程图像尺寸的方法

我正在使用imagesize gem来检查远程图像的大小,然后只将足够大的图像推送到数组中。 require ‘open-uri’ require ‘image_size’ data = Nokogiri::HTML(open(url)) images = [] forcenocache = Time.now.to_i # No cache because jquery load event doesn’t fire for cached images data.css(“img”).each do |image| image_path = URI.join(site, URI.encode(image[:src])) open(image_path, “rb”) do |fh| image_size = ImageSize.new(fh.read).get_size() unless image_size[0] < 200 || image_size[1] < 100 image_element = "” images.push(image_element) end end […]

ruby中的function代码示例

我正在寻找ruby中的function代码示例。 也许你知道一些gem,在哪里可以找到这样的代码?

从URL下载图像?

我正在尝试使用HTTP :: get从我创建的URL下载Google图表的图像。 这是我的第一次尝试: failures_url = [title, type, data, size, colors, labels].join(“&”) require ‘net/http’ Net::HTTP.start(“http://chart.googleapis.com”) { |http| resp = http.get(“/chart?#{failures_url”) open(“pie.png” ,”wb”) { |file| file.write(resp.body) } } 其中只生成一个空的PNG文件。 对于我的第二次尝试,我在http.get()调用中使用了存储在failure_url内的值。 require ‘net/http’ Net::HTTP.start(“http://chart.googleapis.com”) { |http| resp = http.get(“/chart?chtt=Builds+in+the+last+12+months&cht=bvg&chd=t:296,1058,1217,1615,1200,611,2055,1663,1746,1950,2044,2781,1553&chs=800×375&chco=4466AA&chxl=0:|Jul-2010|Aug-2010|Sep-2010|Oct-2010|Nov-2010|Dec-2010|Jan-2011|Feb-2011|Mar-2011|Apr-2011|May-2011|Jun-2011|Jul-2011|2:|Months|3:|Builds&chxt=x,y,x,y&chg=0,6.6666666666666666666666666666667,5,5,0,0&chxp=3,50|2,50&chbh=23,5,30&chxr=1,0,3000&chds=0,3000”) open(“pie.png” ,”wb”) { |file| file.write(resp.body) } } 并且,出于某种原因,即使第一次尝试在http.get()调用中具有相同的数据,此版本仍然有效。 有人知道为什么吗? 解: 在试图弄清楚为什么会发生这种情况后,我发现“ 如何通过HTTP下载二进制文件? ”。 其中一条评论提到在Net::HTTP.start(…)调用中删除http://否则它将不会成功。 我这样做之后果然: failures_url = [title, […]

表格提交两次,原因是:remote => true

我的表单提交了两次,经过双重检查,它是由’:remote => true’引起的。 我删除它,我的项目运作良好。 谁能告诉我为什么? 以及如何使用’:remote => true’? 我的ruby代码: true, :id => ‘new_product_group_form’) do %> [:product_scopes, :groups, group_name]), scopes.keys.map do |scope_name| [ t(:name, :scope => [:product_scopes, :scopes, scope_name]), scope_name] end ] end ) %> <input type="submit" value="” /> 浏览器中的最终html代码。 Add a scope In Taxon(without descendants) In taxons and all their descendantsProduct name have following Product […]

在Ruby中使用Symbol#to_proc速记链接方法?

我想知道有没有办法可以使用(&:方法)链接方法 例如: array.reject { |x| x.strip.empty? } 把它变成: array.reject(&:strip.empty?) 由于其可读性,我更喜欢简写符号。

这是什么意思?

如以下代码来自“为什么的尖锐指南”: def wipe_mutterings_from( sentence ) unless sentence.respond_to? :include? raise ArgumentError, “cannot wipe mutterings from a #{ sentence.class }” end while sentence.include? ‘(‘ open = sentence.index( ‘(‘ ) close = sentence.index( ‘)’, open ) sentence[open..close] = ” if close end end