Questions tagged [activesupport]

Active Support is the Ruby on Rails component responsible for providing Ruby language extensions, utilities, and other transversal stuff.

Active Support is the Ruby on Rails component responsible for providing Ruby language extensions, utilities, and other transversal stuff.

It offers a richer bottom-line at the language level, targeted both at the development of Rails applications, and at the development of Ruby on Rails itself.

575 questions
182
votes
12 answers

Count number of days between two dates

How do I count the number of days between these two dates? start_date = Date.parse "2012-03-02 14:46:21 +0100" end_date = Date.parse "2012-04-02 14:46:21 +0200"
andkjaer
  • 3,995
  • 10
  • 36
  • 45
120
votes
7 answers

How to add 10 days to current time in Rails

I tried doing something like Time.now + 5.days but that doesn't work, even though I vaguely remember seeing, and being very impressed, with being able to do something like 2.years etc. How do I do that in Rails 3?
Yuval Karmi
  • 26,277
  • 39
  • 124
  • 175
119
votes
2 answers

What is mattr_accessor in a Rails module?

I couldn't really find this in Rails documentation but it seems like 'mattr_accessor' is the Module corollary for 'attr_accessor' (getter & setter) in a normal Ruby class. Eg. in a class class User attr_accessor :name def set_fullname @name…
JasonOng
  • 3,268
  • 3
  • 21
  • 17
106
votes
5 answers

How to use Active Support core extensions

I have Active Support 3.0.3 installed and Rails 3.0.3 with Ruby 1.8.7. When I try to use 1.week.ago I get NoMethodError: undefined method 'week' for 1:Fixnum from (irb):2 The other core extensions seem to work. I tried it on a friend's computer…
griotspeak
  • 13,022
  • 13
  • 43
  • 54
102
votes
6 answers

Convert UTC to local time in Rails 3

I'm having trouble converting a UTC Time or TimeWithZone to local time in Rails 3. Say moment is some Time variable in UTC (e.g. moment = Time.now.utc). How do I convert moment to my time zone, taking care of DST (i.e. using EST/EDT)? More…
Marc-André Lafortune
  • 78,216
  • 16
  • 166
  • 166
61
votes
6 answers

How to encode media in base64 given URL in Ruby

I'm trying to upload an image to PingFM. Their documentation says: media – base64 encoded media data. I can access this image via the URL. I tried (practically guessed) this: ActiveSupport::Base64.encode64(open("http://image.com/img.jpg")) But I…
Ramon Tayag
  • 15,224
  • 9
  • 43
  • 69
58
votes
2 answers

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

I am wanting to test a function on one of my models that throws specific errors. The function looks something like this: def merge(release_to_delete) raise "Can't merge a release with itself!" if( self.id == release_to_delete.id ) raise "Can…
spilliton
  • 3,811
  • 5
  • 35
  • 35
56
votes
4 answers

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

So in Rails 3.2, ActiveSupport::Memoizable has been deprecated. The message reads: DEPRECATION WARNING: ActiveSupport::Memoizable is deprecated and will be removed in future releases,simply use Ruby memoization pattern instead. It refers to "Ruby…
bodacious
  • 6,608
  • 9
  • 45
  • 74
52
votes
1 answer

How to use Rails 3's ActiveSupport core extensions outside Rails

I'm having a problem using ActiveSupport's core extensions on a gem I am developing. I had it working with AS 2.3.8, but as soon as I wanted to port it to 3b4, the extensions stopped working and my test results are filled with lines such as:…
changelog
  • 4,646
  • 4
  • 35
  • 62
46
votes
6 answers

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

When upgrading to ruby 3.1, I am seeing the following sort error message when using YAML.load_file some_file_name Psych::DisallowedClass: Tried to load unspecified class: Matrix Other load statements cause similar errors but cite different…
Obromios
  • 15,408
  • 15
  • 72
  • 127
45
votes
6 answers

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

I have set the time zone in /config/application.rb, and I expect all times generated in my app to be in this time zone by default, yet when I create a new DateTime object (using .new), it creates it in GMT. How can I get it to be in my app's time…
user664833
  • 18,397
  • 19
  • 91
  • 140
44
votes
7 answers

How can I create a new Date instance in Ruby

How can I create a new Date object in IRB with a given date. The following didn't work. 1.9.3p194 :053 > require 'active_support' => true 1.9.3p194 :054 > Date.new => # 1.9.3p194 :055 > Date.parse('12/01/2012') NoMethodError:…
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
36
votes
9 answers

How do I avoid the circular argument reference warning in activesupport

How do I avoid the circular argument reference warning in activesupport. Happens on ruby 2.2.0 /home/ec2-user/apps/foo_prod/shared/bundle/ruby/2.2.0/gems/activesupport-3.2.21/lib/active_support/values/time_zone.rb:270: warning: circular argument…
Rpj
  • 5,348
  • 16
  • 62
  • 122
36
votes
3 answers

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

I have a certain module which is used in a Rails 4.1.1 application module A extend ActiveSupport::Concern included do #Some code end end which is included in a class class Some include A end This works great with…
muichkine
  • 2,890
  • 2
  • 26
  • 36
36
votes
1 answer

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

How can a method take the current Time.zone and put it into a format that is usable by ActiveSupport::TimeZone[some_zone].parse()? It seems very strange that Time.zone.to_s returns a string that can not be used with…
jpw
  • 18,697
  • 25
  • 111
  • 187
1
2 3
38 39