Commit Graph

1665 Commits

Author SHA1 Message Date
Akshay Birajdar
26575aa19c
Support rfc2822 format for Time#to_fs & Date#to_fs (#52337) 2024-07-16 12:34:20 +02:00
John Hawthorn
725ebc9e10 Remove code for DateTime-backed TimeWithZone
At one point (I believe until ruby-1.8.0) Time could only represent
values between 1970 and the integer overflow in 2038. On modern Ruby
there does not seem to be a limit.

    >> Time.at(2**128)
    => 10783118943836478994022445751222-08-06 01:04:16 -0700

TimeWithZone will also convert a DateTime to a Time when initialized
with one, so the code we had to catch this overflow and to deal with
DateTime is dead. This commit removes this code and adjusts the test to
be more general (the old test passed but we might as well keep a better
version of the test to check that we have a large both negative and
positive range).

Co-authored-by: Adam Hess <HParker@github.com>
2024-07-11 10:34:51 -07:00
Rafael Mendonça França
716e4a7d32
Merge pull request #52091 from jasonkim/to-time-use-timezone
Add a config for preserving timezone information when calling `to_time` on TimeWithZone object
2024-06-26 16:16:05 -04:00
Rafael Mendonça França
f46d06b3b5
Merge pull request #52031 from matthewd/quieter-to_time
Don't emit to_time deprecations in known-safe contexts
2024-06-26 16:09:59 -04:00
Jason Kim
9dcf17ec4c Add a config for preserving timezone information
when calling `to_time` on TimeWithZone object

Co-authored-by: jhawthorn <jhawthorn@github.com>
2024-06-13 14:56:40 -07:00
John Hawthorn
2627c953cb
Avoid using to_time in TimeWithZone#-
We're more likely to already have utc and can avoid constructing the
extra time object, and this will avoid deprecations on unconfigured
to_time_preserves_timezone.
2024-06-13 19:37:54 +00:00
Igor Depolli
4813a0f036
[ActiveSupport] Add option filter on in_order_of (#52072)
* [ActiveSupport] Add option 'filter' on 'in_order_of'

* Update activesupport/lib/active_support/core_ext/enumerable.rb

Co-authored-by: Willian Gustavo Veiga <willianveiga@users.noreply.github.com>

---------

Co-authored-by: Willian Gustavo Veiga <willianveiga@users.noreply.github.com>
Co-authored-by: Rafael Mendonça França <rafael@rubyonrails.org>
2024-06-12 15:13:50 -07:00
Matthew Draper
d47c37183a Don't emit to_time deprecations in known-safe contexts
If the value is already a local time, there's no difference, so no need
to warn.

Correspondingly, avoid calling to_time in the handful of places we were
using it internally: it's easy to do, and we know we don't care about
the zone.
2024-06-11 15:51:46 -07:00
Matthew Draper
595c3ccc47 Re-roll deprecation of to_time_preserves_timezone
We realised the previous deprecation hadn't been warning for all users.
2024-06-03 00:57:09 +09:30
Matthew Draper
43fcdfa4d0 Revert "Remove deprecated support for the pre-Ruby 2.4 behavior of to_time"
This reverts commit b1b2c6d59c3e82ff8eed95f5556a902a4c03abd9.
2024-06-02 23:52:59 +09:30
Xavier Noria
a7d7e3d884 Define Digest::UUID.nil_uuid 2024-05-29 14:41:30 +02:00
nikhilbhatt
575c6413bb Fix exception raised from template should not show compiled code 2024-05-20 17:07:19 +09:00
Rafael Mendonça França
be211227a0
Remove deprecated #to_default_s from Array, Date, DateTime and Time 2024-05-01 18:45:41 +00:00
Rafael Mendonça França
b1b2c6d59c
Remove deprecated support for the pre-Ruby 2.4 behavior of to_time
When `to_time_preserves_timezone` was set to `false`, `to_time`
would return a `Time` object with local timezone. This behavior
has been removed.
2024-05-01 18:44:31 +00:00
Rosa Gutierrez
71a74ad035
Preserve encoding on truncate_bytes (#51313)
String.new with no arguments returns the empty string with ASCII-8BIT
encoding. Then, depending on each grapheme cluster of the string and
on the omission string, the resulting string might keep the ASCII-8BIT
encoding. With this change, we preserve the encoding of the original
string instead.

Note that String.new accepts an `encoding` keyword argument, like
```
String.new(encoding: Encoding::UTF_8)
```
However, instead of using that, we rely on `force_encoding` to set the
original encoding. This is so that String subclasses don't need to
preserve this keyword argument. For example, SafeBuffer doesn't.
Thanks to @jeremy for catching this!
2024-03-12 15:39:21 -07:00
Jean Boussier
50daadaa71 Update test suite for compatibility with Ruby 3.4-dev
https://bugs.ruby-lang.org/issues/19117 and https://bugs.ruby-lang.org/issues/16495
slightly change how backtrace are rendered which makes a few tests fail.
2024-02-16 11:55:44 +01:00
Jean Boussier
ca6995a80c Update Method#duplicable? to be consistent with Ruby 3.4
Fix: https://github.com/rails/rails/issues/51075

`Method` and `UnboundMethod` used to raise on `#dup`, but not `#clone`,
this wasn't so much a feature, but a bug.

It was fixed in https://github.com/ruby/ruby/pull/9926.
2024-02-14 09:52:55 +01:00
George Ma
3711c69ab0 Add :: to namespace the module we delegate "to"
Only namespace when the delegation object is a module
2024-01-31 11:35:15 +01:00
Yasuo Honda
33c6f0f723 Require 'uri' explicitly to address NameError: uninitialized constant ToQueryTest::URI
This commit addresses CI failure
at https://buildkite.com/rails/rails-nightly/builds/108#018d57ac-4f2a-45f1-86b9-9015a7b0a463/1165-2002

* Error addressed by this commit
```
$ ruby -v
ruby 3.4.0dev (2024-01-30T10:19:23Z master 86547fd69d) [x86_64-linux]
$ rm Gemfile.lock
$ bundle install
$ cd activesupport
$ bin/test test/core_ext/object/to_query_test.rb -n test_hash_not_sorted_lexicographically_for_nested_structure
... snip ...

E

Error:
ToQueryTest#test_hash_not_sorted_lexicographically_for_nested_structure:
NameError: uninitialized constant ToQueryTest::URI
    test/core_ext/object/to_query_test.rb:90:in `test_hash_not_sorted_lexicographically_for_nested_structure'

bin/test test/core_ext/object/to_query_test.rb:79

Finished in 0.000385s, 2597.3082 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
$
```

This behavior has been introduced since Ruby 3.4.0dev vendors URI from top level to `Gem::URI`
via d64d0b5423 .
Here is the RubyGems one.
https://github.com/rubygems/rubygems/pull/7386
2024-01-30 20:29:33 +09:00
Jean Boussier
946e46ebcc Modernize method missing implementations
`...` is both simpler an more correct since the keyword argument
separation.
2024-01-16 13:17:45 +01:00
Sean Doyle
9e64b13d8a Yield instance to Object#with block
The introduction of the block argument means that `Object#with` can now
accept a `Symbol#to_proc` as the block argument:

```ruby
client.with(timeout: 5_000) do |c|
  c.get("/commits")
end
```
2024-01-07 20:56:33 +01:00
Rafael Mendonça França
61e2b9a415
Put back the require 2024-01-04 15:18:47 +00:00
Rafael Mendonça França
2bbdb7c3d3
Force skip in test/core_ext/object/json_cherry_pick_test.rb
Those tests should be skipped if the json core_ext is already loaded.
2024-01-04 04:09:41 +00:00
Rafael Mendonça França
391229eddf
Fix force_skip patch 2024-01-04 04:04:05 +00:00
Rafael Mendonça França
ad343c5034
Allow to force skip tests
This is useful for when a test depends on code not being loaded that
might be loaded by another test.
2024-01-04 03:34:07 +00:00
Jean Boussier
be258503ac Module#delegate takes a new private as parameter
This is a continuation of https://github.com/rails/rails/pull/46875

The behavior of looking up the class method when `to: :class` is passed
is a bit error prone because it silently degrades.

By passing the expected owner of the delegated method, we can be more
strict, and also generate a delegator in a module rather than having
to do it at inclusion time.

I made this argument private API because we want it in Rails, but
I'm worried it might be a bit too sharp for public API. I can
be convinced otherwise though.
2023-12-08 15:09:59 +01:00
Jean Boussier
db92ea32e0 Simplify attr_internal_define
The `@` prefix is always stripped, so might as well not require it.

For backward compatibility reasons we still handle the prefix for now,
but we eagerly strip it and emit a deprecation.
2023-10-31 13:42:57 +01:00
Jonathan Hefner
bb17d787c8 Ensure {down,up}case_first returns non-frozen string
Prior to this commit, `String#downcase_first` and `String#upcase_first`
would return a frozen string when called on an empty string:

  ```ruby
  # BEFORE
  "foo".downcase_first.frozen? # => false
  "".downcase_first.frozen?    # => true

  # AFTER
  "foo".downcase_first.frozen? # => false
  "".downcase_first.frozen?    # => false
  ```
2023-10-29 12:28:06 -05:00
Earlopain
c6dcb11691
Handle negative numbers in NumberToHumanSizeConverter 2023-10-26 18:07:15 +02:00
Matt Brictson
ac4c4c40a3
Fix Object.with test class name typo
`with_test.rb` should define a class named `WithTest`. Instead, it
redefines `BlankTest`. This seems to be a copy/paste typo.

This commit renames `BlankTest` to `WithTest` to follow convention, and
to avoid any potential conflicts of 2 test files defining the same
class.
2023-10-24 07:51:40 -07:00
yuuji.yaginuma
bbe591d8f4 Don't use deprecated #to_default_s in Date#to_fs
In Rails 7.1.1, using `Date#to_fs` with an un-exist format, shows the
deprecation warning like the following.

```ruby
Date.current.to_fs(:doesnt_exist)
=> DEPRECATION WARNING: to_default_s is deprecated and will be removed from Rails 7.2 (use to_s instead) (called from <main> at ./bin/rails:4)
```

But other date-time-related classes still allow to use this.

6f6c211f47/activesupport/lib/active_support/core_ext/time/conversions.rb (L57)
6f6c211f47/activesupport/lib/active_support/core_ext/date_time/conversions.rb (L39)

So I thought this was just a typo and fixed to use `#to_s` like
other classes.
2023-10-22 17:17:42 +09:00
Nikita Vasilevsky
19f8ab2e7d
[Tests only] Enable Minitest/AssertPredicate rule 2023-10-13 19:26:47 +00:00
Hartley McGuire
64184c6f50
Fix Range#overlap? ignoring empty ranges
Previously, #overlap? would incorrectly return true when one of the
ranges is effectively "empty":

```ruby
(2...2).overlap? 1..2 # => true
(1..2).overlap? 2...2 # => true
```

This is fixed in the Ruby 3.3 implementation of Range#overlap?, so this
commit fixes it for Ruby < 3.3 as well.

The tests added are from the Ruby repository and the implementation is
effectively a Ruby version of the fix in C.

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
Co-authored-by: Shouichi Kamiya <shouichi.kamiya@gmail.com>
2023-09-22 19:48:49 -04:00
Shouichi Kamiya
51ac8b9f6f Enable Minitest/LiteralAsActualArgument
There are assertions that expected/actual arguments are passed in the
reversed order by mistake. Enabling the LiteralAsActualArgument rule
prevents this mistake from happening.

The existing tests were auto-corrected by rubocop with a bit of
indentation adjustment.

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-09-13 10:09:32 +09:00
Petrik
9aeffae14f Use SecureRandom.alphanumeric for SecureRandom.base36
Ruby 3.3 allows passing a list of characters to
`SecureRandom.alphanumeric`. For `SecureRandom.base36` using `choose` is
faster than the current implementation. For `SecureRandom.base58` it is
a bit slower.

I've also added a test to make sure passing nil as the length defaults
the length to 16.

_Benchmark__

```ruby

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", github: "rails/rails", branch: "main"
  gem "benchmark-ips"
end

require "active_support"
require "active_support/core_ext/securerandom"

module SecureRandom
  def self.fast_base36(n)
    alphanumeric(n, chars: BASE36_ALPHABET)
  end
end

[10, 100, 1000, 10000].each do |length|
  puts
  puts " #{length} ".center(80, "=")
  puts

  Benchmark.ips do |x|
    x.report("base36")      { SecureRandom.base36(length) }
    x.report("fast_base36") { SecureRandom.fast_base36(length) }
    x.compare!
  end
end
```

```
====================================== 10 ======================================

Warming up --------------------------------------
              base36    20.513k i/100ms
         fast_base36    24.843k i/100ms
Calculating -------------------------------------
              base36    200.940k (±13.8%) i/s -    984.624k in   5.060203s
         fast_base36    235.531k (± 5.7%) i/s -      1.192M in   5.080574s

Comparison:
         fast_base36:   235530.9 i/s
              base36:   200939.9 i/s - same-ish: difference falls within error

===================================== 100 ======================================

Warming up --------------------------------------
              base36     2.746k i/100ms
         fast_base36     2.995k i/100ms
Calculating -------------------------------------
              base36     25.559k (± 8.5%) i/s -    129.062k in   5.087961s
         fast_base36     30.265k (± 6.6%) i/s -    152.745k in   5.070263s

Comparison:
         fast_base36:    30264.7 i/s
              base36:    25558.8 i/s - 1.18x  slower

===================================== 1000 =====================================

Warming up --------------------------------------
              base36   278.000  i/100ms
         fast_base36   308.000  i/100ms
Calculating -------------------------------------
              base36      2.595k (±11.6%) i/s -     12.788k in   5.007921s
         fast_base36      3.133k (± 6.1%) i/s -     15.708k in   5.033310s

Comparison:
         fast_base36:     3132.6 i/s
              base36:     2594.9 i/s - 1.21x  slower

==================================== 10000 =====================================

Warming up --------------------------------------
              base36    24.000  i/100ms
         fast_base36    34.000  i/100ms
Calculating -------------------------------------
              base36    256.601  (± 8.6%) i/s -      1.296k in   5.089604s
         fast_base36    322.119  (± 6.5%) i/s -      1.632k in   5.089614s

Comparison:
         fast_base36:      322.1 i/s
              base36:      256.6 i/s - 1.26x  slower

```
2023-08-30 11:28:42 +02:00
zzak
cbe2a4c52c
Use a consistent format when testing BigDecimal.to_s
The previous examples were causing failures in CI, after ruby/bigdecimal#264 was merged.

For example:

```
Failure:
NumericExtFormattingTest#test_default_to_fs [/rails/activesupport/test/core_ext/numeric_ext_test.rb:446]:
--- expected
+++ actual
@@ -1 +1,3 @@
-"10000 10.0"
+# encoding: US-ASCII
+#    valid: true
+"10 00010.0"
```

The recommendation was to adjust the test to deal with the upstream change more flexibly.
2023-07-09 17:22:21 +09:00
Christian Schmidt
11ed5b5c52 Rename Range#overlaps? to Range#overlap?
Rename for consistency with #include? and #cover?
2023-06-23 10:25:59 +02:00
Alex
3bfede1f22 Support beginless and endless ranges in to_fs
Fixes https://github.com/rails/rails/issues/48485
2023-06-16 16:47:48 +10:00
Rafael Mendonça França
aff1db4100
Merge pull request #48412 from andrewn617/fix_defect_in_enumerable_many
Fix defect in Enumerable#many introduced in rails/rails@d862dff
2023-06-06 18:03:33 -04:00
Andrew Novoselac
745976bd57 Fix defect in Enumerable#many introduced in rails/rails@d862dff
This changed `many?` to yield with `element, *args`. The causes elements that are arrays to be destructured which can lead to defective behaviour. Since the introduction of `element, *args` was for readability purposes, we can just use `*args` instead and get the same behaviour without the defect.
2023-06-06 17:41:44 -04:00
Nikita Vasilevsky
e583f15a4e
Use native assert_raises instead of custom assert_raises_with_message.
`assert_raises` has been extended with a `match:` keyword argument to
assert the error message so there is no reason for Rails itself to keep
maintaining custom implementations of the same feature.
2023-06-06 20:10:23 +00:00
Jonathan Hefner
a126949215 Test String#blank? with every non-dummy encoding
This adds test coverage for `String#blank?` with every non-dummy
encoding that is bundled with Ruby.
2023-05-07 21:55:59 -05:00
Jean Boussier
e54ef0ab14 Object#deep_dup no longer duplicate named classes and modules.
It makes very little sense to duplicate named classes and modules.

Before:

```ruby
hash = { class: Object, module: Kernel }
hash.deep_dup # => {:class=>#<Class:0x00000001063ffc80>, :module=>#<Module:0x00000001063ffa00>}
```

After:

```ruby
hash = { class: Object, module: Kernel }
hash.deep_dup # => {:class=>Object, :module=>Kernel}
```
2023-05-02 10:54:46 +02:00
Miha Rekar
ffa27f314a
Only flatten first level to preserve nested 2023-04-25 16:56:09 +02:00
Vasiliy Ermolovich
bbb13631f8 Fix delegation to a method with implicit block.
When 8b2f57dc6f was
introduced it broke delegation of a method with implicit block like this:

```ruby
class Foo
  def self.bar
    yield
  end
end

class Bar
  delegate :bar, to: Foo
end

Bar.new.bar { } # raises LocalJumpError: no block given (yield)
```

It happens because it's impossible to detect a method with implicit block and
we rely on method's parameters when we generate delegated methods.

I've found only one solution to this issue: we always generate a method that explicitly accepts
block. It kind of makes sense since evey Ruby method accepts block implicitly anyway.

Fixes https://github.com/rails/rails/issues/47624
2023-04-10 19:28:26 +02:00
Rafael Mendonça França
ddd6f8ecfe
Merge pull request #47771 from a5-stable/add-more-storage-units
Add zettabyte to `number_to_human_size`
2023-03-29 11:52:45 -04:00
fatkodima
ba2a26e083 Fix Enumerable#in_order_of to preserve duplicates 2023-03-29 15:15:14 +03:00
a5-stable
b9a5a4bed8 add zetta to number_to_human_size 2023-03-28 19:50:13 +09:00
Jean Boussier
210932662b Also undefine with on Symbol 2023-03-16 11:04:19 +00:00
Jean Boussier
dc6bfecdef Undefine with on NilClass and other common immediate types
with isn't usable on immediates, so we might as well undefine the
method in common immediate classes to avoid potential confusion.
2023-03-16 10:29:54 +00:00