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

Symbol is also not allowed per default when loading YAML in Ruby. Therefore, you need to add Symbol to the permitted_classes in your case too when reading the YAML file: hash = YAML.load_file( some_file_name, permitted_classes: [Matrix, OpenStruct, Symbol] ) See the list of default permitted_classes in Psych (the YAML parser used by Ruby). Or, 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

Rails ActiveSupport: How to assert that an error is raised?

So unit testing isn’t really in activesupport. Ruby comes with a typical xunit framework in the standard libs (Test::Unit in ruby 1.8.x, MiniTest in ruby 1.9), and the stuff in activesupport just adds some stuff to it. If you are using Test::Unit/MiniTest assert_raise(Exception) { whatever.merge } if you are using rspec (unfortunately poorly documented, but … Read more

How to encode media in base64 given URL in Ruby

To encode a file: require ‘base64’ Base64.encode64(File.open(“file_path”, “rb”).read) To produce the file from the encoded string: require ‘base64’ encoded_string = Base64.encode64(File.open(“file_path”, “rb”).read) File.open(file_name_to_create, “wb”) do |file| file.write(Base64.decode64(encoded_string)) end

Convert UTC to local time in Rails 3

Time#localtime will give you the time in the current time zone of the machine running the code: > moment = Time.now.utc => 2011-03-14 15:15:58 UTC > moment.localtime => 2011-03-14 08:15:58 -0700 Update: If you want to conver to specific time zones rather than your own timezone, you’re on the right track. However, instead of worrying … Read more

How to use Active Support core extensions

Since using Rails should handle this automatically I’m going to assume you’re trying to add Active Support to a non-Rails script. Read “How to Load Core Extensions“. Active Support’s methods got broken into smaller groups in Rails 3, so we don’t end up loading a lot of unneeded stuff with a simple require ‘activesupport’. Now … Read more

What is mattr_accessor in a Rails module?

Rails extends Ruby with both mattr_accessor (Module accessor) and cattr_accessor (as well as _reader/_writer versions). As Ruby’s attr_accessor generates getter/setter methods for instances, cattr/mattr_accessor provide getter/setter methods at the class or module level. Thus: module Config mattr_accessor :hostname mattr_accessor :admin_email end is short for: module Config def self.hostname @hostname end def self.hostname=(hostname) @hostname = hostname … Read more