当变量超出范围时,是否意味着它不存在?

我不确定我是否理解范围 – 一个超出范围的变量(我正在使用Ruby)存在于某个地方的内存中,或者它是否停止存在(我知道你无法访问它)。 说超出范围的变量不再存在是否不准确? 也许这是一个哲学问题。

跨越检查板算法改进

由于David Kahn的书“ The Codebreakers” ,我正在尝试使用Ruby类来处理旧密码,例如Nihilist密码和ADFGVX,古代密码学是我的爱好之一。 对于这些,一个有用的项目是跨越检查板 。 我在Ruby中有以下实现,并欢迎任何改进。 class Key是基类(如果需要,可以是虚拟类)。 Key#condensed是一种从给定单词中删除重复字母的方法。 class SKey < Key attr_reader :full_key attr_reader :alpha, :ralpha def initialize(key) super(key) @alpha = Hash.new @ralpha = Hash.new @full_key = checkboard() gen_rings() end # === checkboard # # Shuffle the alphabet a bit to avoid sequential allocation of the # code numbers # # […]

获取按关联字段分组的所有记录,并按组中的计数排序

我有3个型号: Post , Comment , User Post有很多Comments Comment属于User User有字段country_code 我想获得按国家/地区代码分组的所有post评论,并按每个国家/地区的评论数量排序。 这个查询: post.comments.joins(:user).group(“users.country_code”).order(‘count_all desc’).count 返回这样的结果: {“DE”=>67, “US”=>8, “RS”=>8, “IN”=>8, “ES”=>7, “BR”=>6, … “UA”=>0 } 我需要的是一个类似的结果,其中国家代码是键,但值是注释数组。 我不知道如何实现这一目标。

在Windows上安装捆绑包时出错

当我尝试运行bundle install时,我收到此错误: Installing hiredis (0.3.2) with native extensions Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension. C:/RailsInstaller/Ruby1.9.2/bin/ruby.exe extconf.rb gcc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb net.c net.c:1:0: warning: -fPIC ignored for target (all code is position independent) net.c:35:24: fatal error: sys/socket.h: No such file or directory compilation terminated. make: *** […]

如何通过连接表中的条件接收对象

我有桌子Games和Badges他们通过表BadgesGames连接。 t.integer “badge_id” t.integer “game_id” t.boolean “shown”, default: false 我有Game对象,我想要找到所有未shown Badges game_object.(condition?).badges 怎么弄呢?

你如何从Sinatra中删除路线?

我有一些动态加载的插件,它们在启动时注册它们的路由,但是我还需要能够在它们被禁用时删除它们的路由。 有没有办法删除现有路线? API没有任何我可以找到的方法来删除它们,我能想到的另一种方法就是直接进入Sinatra :: Base的@routes对象,但我不确定你是否可以做任何事情,如果可以……这样做是否安全?

Rails:时区之间的差异,以小时为单位

我希望能够说user1 is 4 hours ahead of user2 ,根据用户在其帐户中指定的时区计算。 使用以下代码: time1 = Time.zone.now.in_time_zone(user1.time_zone) time2 = Time.zone.now.in_time_zone(user2.time_zone) distance_of_time_in_words time1,time2 …给出less than a minute的差异 – 同样减去两次给出0 。 Rails显然仍然认为这两次是相同的。 知道如何计算两个时区之间的差异吗?

将Object转换为hash,然后将其保存到用户列

无法找到我正在尝试做的事情。 我想将一个对象存储到用户的列中。 该列采用数组的forms: #postgres def change add_column :users, :interest, :string, array: true, default: ‘{}’ end 我有另一个名为FooBar的模型用于其他用途。 当我添加了user_id键时,每个用户都有唯一的信息。 我想要更有意义: def interest @user = User.find(current_user.id ) # I need the logged in user’s id @support = Support.find(params[:id]) # I need the post’s id they are on u = FooBar.new u.user_id = @user u.support_id = @support u.save # […]

Rails 3.1 named_scope

编写下面代码的Rails 3.1是什么: named_scope :min_2_items_last_90_days, { :include => { :orders => :order_items }, :conditions => [‘orders.created_at >= ?’, 90.days.ago], :group => ‘people.id’, :having => ‘COUNT(order_items.id) >= 2’ }

有没有更好的方法来获取Ruby对象的公共“属性”?

有没有更好的方法来获取Ruby对象的公共“属性”? def props self.public_methods.grep(/.=$/) – [“==”,”===”] end