Rails: how get current time zone compatible with ActiveSupport::TimeZone[zone].parse()?

Use Time.zone.name, not Time.zone.to_s [1] pry(main)> Time.zone.to_s => “(GMT-05:00) Eastern Time (US & Canada)” [2] pry(main)> Time.zone.name => “Eastern Time (US & Canada)” [3] pry(main)> ActiveSupport::TimeZone[Time.zone.name] => (GMT-05:00) Eastern Time (US & Canada) As for how I got this (as requested), I just know the name method exists on Time.zone. If I didn’t know this … Read more

Cannot define multiple ‘included’ blocks for a Concern (ActiveSupport::Concern::MultipleIncludedBlocks) with cache_classes = true

For anyone hitting the same wall to read, the solution to this is to strictly respect Rails autoloading rules. That is Removing all the require / require_relative Add needed paths to Rails autoload paths Put files at the right places with the right names so Rails can infer where to look for code to load. … Read more

Upgrading to Ruby 3.1 causes Psych::DisallowedClass exception when using YAML.load_file

Symbol is also not allowed per default. Therefore, you need to add Symbol to the permitted_classes too when loading a file: hash = YAML.load_file( some_file_name, permitted_classes: [Matrix, OpenStruct, Symbol] ) See the list of default permitted_classes in Psych. Or, when using in Ruby on Rails, you can configure what classes Rails should permit globally when … Read more

How can I create a new Date instance in Ruby

According to Date documentation: require ‘date’ Date.new(2001,2,25) #=> #<Date: 2001-02-25 Date.jd(2451966) #=> #<Date: 2001-02-25 Date.ordinal(2001,56) #=> #<Date: 2001-02-25 Date.commercial(2001,8,7) #=> #<Date: 2001-02-25 Date.parse(‘2001-02-25′) #=> #<Date: 2001-02-25 Date.strptime(’25-02-2001’, ‘%d-%m-%Y’) #=> #<Date: 2001-02-25 Time.new(2001,2,25).to_date #=> #<Date: 2001-02-25

How to create a new DateTime object in a specific time zone (preferably the default time zone of my app, not UTC)?

You can use ActiveSupport’s TimeWithZone (Time.zone) object to create and parse dates in the time zone of your application: 1.9.3p0 :001 > Time.zone.now => Wed, 11 Jul 2012 19:47:03 PDT -07:00 1.9.3p0 :002 > Time.zone.parse(‘2012-07-11 21:00’) => Wed, 11 Jul 2012 21:00:00 PDT -07:00

Which Ruby memoize pattern does ActiveSupport::Memoizable refer to?

Here is the commit (and subsequent discussion) where Memoizable was deprecated: https://github.com/rails/rails/commit/36253916b0b788d6ded56669d37c96ed05c92c5c The author advocates the @foo ||= … approach and points to this commit as an example for migration: https://github.com/rails/rails/commit/f2c0fb32c0dce7f8da0ce446e2d2f0cba5fd44b3. Edit: Note that I don’t necessarily interpret this change as meaning that all instances of memoize can or should be replaced w/ this pattern. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)