Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into dev/gc-mmtk
Browse files Browse the repository at this point in the history
  • Loading branch information
peterzhu2118 committed Oct 18, 2024
2 parents 3cf62aa + fbabe13 commit 583ae06
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 34 deletions.
55 changes: 26 additions & 29 deletions array.c
Original file line number Diff line number Diff line change
Expand Up @@ -3556,28 +3556,24 @@ sort_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, dummy))

/*
* call-seq:
* array.sort_by! {|element| ... } -> self
* array.sort_by! -> new_enumerator
* sort_by! {|element| ... } -> self
* sort_by! -> new_enumerator
*
* Sorts the elements of +self+ in place,
* using an ordering determined by the block; returns self.
* With a block given, sorts the elements of +self+ in place;
* returns self.
*
* Calls the block with each successive element;
* sorts elements based on the values returned from the block.
*
* For duplicates returned by the block, the ordering is indeterminate, and may be unstable.
*
* This example sorts strings based on their sizes:
* sorts elements based on the values returned from the block:
*
* a = ['aaaa', 'bbb', 'cc', 'd']
* a.sort_by! {|element| element.size }
* a # => ["d", "cc", "bbb", "aaaa"]
*
* Returns a new Enumerator if no block given:
* For duplicate values returned by the block, the ordering is indeterminate, and may be unstable.
*
* a = ['aaaa', 'bbb', 'cc', 'd']
* a.sort_by! # => #<Enumerator: ["aaaa", "bbb", "cc", "d"]:sort_by!>
* With no block given, returns a new Enumerator.
*
* Related: see {Methods for Assigning}[rdoc-ref:Array@Methods+for+Assigning].
*/

static VALUE
Expand Down Expand Up @@ -7928,40 +7924,41 @@ finish_exact_sum(long n, VALUE r, VALUE v, int z)

/*
* call-seq:
* array.sum(init = 0) -> object
* array.sum(init = 0) {|element| ... } -> object
* sum(init = 0) -> object
* sum(init = 0) {|element| ... } -> object
*
* When no block is given, returns the object equivalent to:
* With no block given, returns the sum of +init+ and all elements of +self+;
* for array +array+ and value +init+, equivalent to:
*
* sum = init
* array.each {|element| sum += element }
* sum
*
* For example, <tt>[e1, e2, e3].sum</tt> returns <tt>init + e1 + e2 + e3</tt>.
* For example, <tt>[e0, e1, e2].sum</tt> returns <tt>init + e0 + e1 + e2</tt>.
*
* Examples:
*
* a = [0, 1, 2, 3]
* a.sum # => 6
* a.sum(100) # => 106
* [0, 1, 2, 3].sum # => 6
* [0, 1, 2, 3].sum(100) # => 106
* ['abc', 'def', 'ghi'].sum('jkl') # => "jklabcdefghi"
* [[:foo, :bar], ['foo', 'bar']].sum([2, 3])
* # => [2, 3, :foo, :bar, "foo", "bar"]
*
* The elements need not be numeric, but must be <tt>+</tt>-compatible
* with each other and with +init+:
* The +init+ value and elements need not be numeric, but must all be <tt>+</tt>-compatible:
*
* a = ['abc', 'def', 'ghi']
* a.sum('jkl') # => "jklabcdefghi"
* # Raises TypeError: Array can't be coerced into Integer.
* [[:foo, :bar], ['foo', 'bar']].sum(2)
*
* When a block is given, it is called with each element
* and the block's return value (instead of the element itself) is used as the addend:
* With a block given, calls the block with each element of +self+;
* the block's return value (instead of the element itself) is used as the addend:
*
* a = ['zero', 1, :two]
* s = a.sum('Coerced and concatenated: ') {|element| element.to_s }
* s # => "Coerced and concatenated: zero1two"
* ['zero', 1, :two].sum('Coerced and concatenated: ') {|element| element.to_s }
* # => "Coerced and concatenated: zero1two"
*
* Notes:
*
* - Array#join and Array#flatten may be faster than Array#sum
* for an +Array+ of Strings or an +Array+ of Arrays.
* for an array of strings or an array of arrays.
* - Array#sum method may not respect method redefinition of "+" methods such as Integer#+.
*
*/
Expand Down
1 change: 1 addition & 0 deletions lib/bundler/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ def binstubs(*gems)
method_option "branch", type: :string
method_option "ref", type: :string
method_option "glob", type: :string, banner: "The location of a dependency's .gemspec, expanded within Ruby (single quotes recommended)"
method_option "quiet", type: :boolean, banner: "Only output warnings and errors."
method_option "skip-install", type: :boolean, banner: "Adds gem to the Gemfile but does not install it"
method_option "optimistic", type: :boolean, banner: "Adds optimistic declaration of version to gem"
method_option "strict", type: :boolean, banner: "Adds strict declaration of version to gem"
Expand Down
2 changes: 2 additions & 0 deletions lib/bundler/cli/add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def initialize(options, gems)
end

def run
Bundler.ui.level = "warn" if options[:quiet]

validate_options!
inject_dependencies
perform_bundle_install unless options["skip-install"]
Expand Down
5 changes: 4 additions & 1 deletion lib/bundler/man/bundle-add.1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
.SH "NAME"
\fBbundle\-add\fR \- Add gem to the Gemfile and run bundle install
.SH "SYNOPSIS"
\fBbundle add\fR \fIGEM_NAME\fR [\-\-group=GROUP] [\-\-version=VERSION] [\-\-source=SOURCE] [\-\-path=PATH] [\-\-git=GIT|\-\-github=GITHUB] [\-\-branch=BRANCH] [\-\-ref=REF] [\-\-skip\-install] [\-\-strict|\-\-optimistic]
\fBbundle add\fR \fIGEM_NAME\fR [\-\-group=GROUP] [\-\-version=VERSION] [\-\-source=SOURCE] [\-\-path=PATH] [\-\-git=GIT|\-\-github=GITHUB] [\-\-branch=BRANCH] [\-\-ref=REF] [\-\-quiet] [\-\-skip\-install] [\-\-strict|\-\-optimistic]
.SH "DESCRIPTION"
Adds the named gem to the [\fBGemfile(5)\fR][Gemfile(5)] and run \fBbundle install\fR\. \fBbundle install\fR can be avoided by using the flag \fB\-\-skip\-install\fR\.
.SH "OPTIONS"
Expand Down Expand Up @@ -36,6 +36,9 @@ Specify the git branch for the added gem\.
\fB\-\-ref\fR
Specify the git ref for the added gem\.
.TP
\fB\-\-quiet\fR
Do not print progress information to the standard output\.
.TP
\fB\-\-skip\-install\fR
Adds the gem to the Gemfile but does not install it\.
.TP
Expand Down
5 changes: 4 additions & 1 deletion lib/bundler/man/bundle-add.1.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ bundle-add(1) -- Add gem to the Gemfile and run bundle install

`bundle add` <GEM_NAME> [--group=GROUP] [--version=VERSION] [--source=SOURCE]
[--path=PATH] [--git=GIT|--github=GITHUB] [--branch=BRANCH] [--ref=REF]
[--skip-install] [--strict|--optimistic]
[--quiet] [--skip-install] [--strict|--optimistic]

## DESCRIPTION

Expand Down Expand Up @@ -41,6 +41,9 @@ Adds the named gem to the [`Gemfile(5)`][Gemfile(5)] and run `bundle install`.
* `--ref`:
Specify the git ref for the added gem.

* `--quiet`:
Do not print progress information to the standard output.

* `--skip-install`:
Adds the gem to the Gemfile but does not install it.

Expand Down
18 changes: 17 additions & 1 deletion lib/ipaddr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,26 @@ def to_string
return str
end

# Returns a string containing the IP address representation with prefix.
def as_json(*)
if ipv4? && prefix == 32
to_s
elsif ipv6? && prefix == 128
to_s
else
cidr
end
end

# Returns a json string containing the IP address representation.
def to_json(*)
format("\"%s\"", as_json)
end

# Returns a string containing the IP address representation in
# cidr notation
def cidr
format("%s/%s", to_s, prefix)
"#{to_s}/#{prefix}"
end

# Returns a network byte ordered string form of the IP address.
Expand Down
4 changes: 2 additions & 2 deletions ractor.c
Original file line number Diff line number Diff line change
Expand Up @@ -3638,7 +3638,7 @@ ractor_local_storage_mark_i(st_data_t key, st_data_t val, st_data_t dmy)
}

static enum rb_id_table_iterator_result
idkey_local_storage_mark_i(ID id, VALUE val, void *dmy)
idkey_local_storage_mark_i(VALUE val, void *dmy)
{
rb_gc_mark(val);
return ID_TABLE_CONTINUE;
Expand All @@ -3661,7 +3661,7 @@ ractor_local_storage_mark(rb_ractor_t *r)
}

if (r->idkey_local_storage) {
rb_id_table_foreach(r->idkey_local_storage, idkey_local_storage_mark_i, NULL);
rb_id_table_foreach_values(r->idkey_local_storage, idkey_local_storage_mark_i, NULL);
}
}

Expand Down
32 changes: 32 additions & 0 deletions spec/bundler/commands/add_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,38 @@
end
end

describe "with --quiet option" do
it "is quiet when there are no warnings" do
bundle "add 'foo' --quiet"
expect(out).to be_empty
expect(err).to be_empty
end

it "still displays warning and errors" do
create_file("add_with_warning.rb", <<~RUBY)
require "#{lib_dir}/bundler"
require "#{lib_dir}/bundler/cli"
require "#{lib_dir}/bundler/cli/add"
module RunWithWarning
def run
super
rescue
Bundler.ui.warn "This is a warning"
raise
end
end
Bundler::CLI::Add.prepend(RunWithWarning)
RUBY

bundle "add 'non-existing-gem' --quiet", raise_on_error: false, env: { "RUBYOPT" => "-r#{bundled_app("add_with_warning.rb")}" }
expect(out).to be_empty
expect(err).to include("Could not find gem 'non-existing-gem'")
expect(err).to include("This is a warning")
end
end

describe "with --strict option" do
it "adds strict version" do
bundle "add 'foo' --strict"
Expand Down
14 changes: 14 additions & 0 deletions test/test_ipaddr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,20 @@ def test_to_s
assert_equal("3ffe:505:2::1", IPAddr.new("3ffe:505:2::1").to_s)
end

def test_as_json
assert_equal("192.168.1.2", IPAddr.new("192.168.1.2").as_json)
assert_equal("192.168.1.0/24", IPAddr.new("192.168.1.2/24").as_json)
assert_equal("2001:200:300::1", IPAddr.new("2001:200:300::1").as_json)
assert_equal("2001:200:300::/48", IPAddr.new("2001:200:300::/48").as_json)
end

def test_to_json
assert_equal("\"192.168.1.2\"", IPAddr.new("192.168.1.2").to_json)
assert_equal("\"192.168.1.0/24\"", IPAddr.new("192.168.1.2/24").to_json)
assert_equal("\"2001:200:300::1\"", IPAddr.new("2001:200:300::1").to_json)
assert_equal("\"2001:200:300::/48\"", IPAddr.new("2001:200:300::/48").to_json)
end

def test_netmask
a = IPAddr.new("192.168.1.2/8")
assert_equal(a.netmask, "255.0.0.0")
Expand Down

0 comments on commit 583ae06

Please sign in to comment.