Tag: 整数

如何计算方法中的方法

如果我们创建一个名为days_left_in_current_level的新方法,那么我们需要在那里放一些,以便我们可以计算current_level中剩余的天数? habit.rb def current_level return 0 unless date_started def committed_wdays committed.map do |day| Date::ABBR_DAYNAMES.index(day.titleize) end end def n_days ((date_started.to_date)..Date.today).count do |date| committed_wdays.include? date.wday end – self.real_missed_days end case n_days # 1 – 6 represent the different levels when 0..9 1 when 10..24 2 when 25..44 3 when 45..69 4 when 70..99 5 else 6 end […]

Rails在参数中序列化整数属性,但不保存到模型实例中

所以我有以下模型,其中包含序列化的整数属性 耙文件: class CreateCompanyAssetStats < ActiveRecord::Migration def change create_table :company_asset_stats do |t| t.integer :companyID t.string :geo t.integer :assetType t.date :startTime t.date :endTime t.timestamps end end end 模型: serialize :companyID, Array serialize :assetType, Array serialize :geo, Array 在我看来,我有一个表单,我有一个多选下拉列表,将值存储到params中: Multi Select <option value=> 生成的参数格式如下:参数:{“utf8”=>“✓”,“authenticity_token”=>“2WFx3 / 3TyHsjobwRaLJYW2jt4JR5ucvtgyBK3IxKTqs =”,“company_asset_stats”=> {“startTime”=>“2013-07-31T00 :00-07:00“,”endTime“=>”2013-07-31T00:00-07:00“,”companyID“=> [”1864“]},”commit“=>”创建“} 最后我的创建控制器执行以下操作: def create @company_asset_stats = CompanyAssetStats.new(params[:company_asset_stats]) if […]

Rails迁移将字符串转换为整数?

是否可以将字符串的字段更改为整数而不清除已输入的数据? 有问题的表的当前db结构是: create_table :people do |t| t.string :company_id 这可能是使用迁移吗? 我想也许在迁移中删除旧字段,创建一个整数的新字段 – 但我担心这将清除已经输入的所有数据。 谢谢, 丹尼

如何修复level.rb使用:承诺的日子?

最初我有:committed日子工作得非常漂亮,但是在更改模型时,为了适应用户的检查能力,如果他错过了:committed一天我现在收到与以下代码相关的错误消息:committed : undefined method to_date for nil:NilClass Line#30 n_days = ((date_started.to_date)..Date.today).count { |date| committed_wdays.include? date.wday } n_days = ((date_started.to_date)..Date.today).count { |date| committed_wdays.include? date.wday } 此代码来自习惯模型: class Habit < ActiveRecord::Base belongs_to :user has_many :levels has_many :days, through: :levels #for being able to access Habit.find(*).days accepts_nested_attributes_for :levels, :days before_save :set_level acts_as_taggable serialize :committed, Array def evaluate(user) levels.each […]

Ruby中的大型乘法输出结果为负

我写了一些代码,它应该为1 <= n <= 1000求和n ^ n。这是代码: sum = 0 (1..1000).each do |n| sum += n**n puts “n = #{n}, sum = #{sum}” end 出于某种原因,在28号之后输出结果为负: n = 29, sum = -2015400977700573523892329442490139437391867 知道为什么会这样吗?

如何整合:错过的日子:养成习惯的日子.rb?

我们如何整合t.integer :missed了t.text :committed这样做 当用户检查他时:missed 3 :committed日期在:level他必须重新启动:level ? 对于每一个:missed一天,他检查,额外的:committed一天被添加回到:level以便他必须在推进之前弥补? 在达到”Mastery”之前,每个习惯都有5个等级! class Habit < ActiveRecord::Base belongs_to :user before_save :set_level acts_as_taggable serialize :committed, Array def self.comitted_for_today today_name = Date::DAYNAMES[Date.today.wday].downcase ids = all.select { |h| h.committed.include? today_name }.map(&:id) where(id: ids) end def levels committed_wdays = committed.map { |day| Date::DAYNAMES.index(day.titleize) } n_days = ((date_started.to_date)..Date.today).count { |date| committed_wdays.include? date.wday } case […]

在Ruby中将整数字符串转换为字节数组

我有一个整数字符串“72101108108111”,但它是一个字符串,表示原始字符串“Hello”的字节。 如何在Ruby中将“72101108108111”转换为Ascii字符串“Hello”?

正则表达式为1-31 Ruby

我试图匹配任何数字1-31(包括)。 这是我最接近的: ([1-9]|[12]\d|3[01]) 但接受324这样的数字。 那里有一个可以捕获1-31的正则表达式的机会吗?

如何仅将数字的字符串哈希值转换为整数

我有从几个不同的XML数据库转储导入的哈希行,看起来像这样(但使用不同的键): {“Id”=>”1”, “Name”=>”Cat”, “Description”=>”Feline”, “Count”=>”123”} 我尝试使用#to_i但它将非数字字符串转换为0 : “Feline”.to_i # => 0 但是我想要的是”Feline”保持字符串的方式,而上面例子中的Id和Count变为整数1和123 。 有没有一种简单的方法可以将数字的字符串值转换为整数?

如何确定Ruby中Fixnum的长度?

在我写的脚本中,我想在Ruby中找到Fixnum的长度。 我可以做.to_s.length ,但有没有办法直接找到Fixnum的长度而不将其转换为String?