From b4f0beb68a051495e51dd4a44a08d8a8d53f6998 Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Mon, 28 May 2012 00:32:17 -0500 Subject: update documentation of array/access methods [ci skip] --- .../lib/active_support/core_ext/array/access.rb | 24 ++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/array/access.rb b/activesupport/lib/active_support/core_ext/array/access.rb index 44d90ef732..a8f9dddae5 100644 --- a/activesupport/lib/active_support/core_ext/array/access.rb +++ b/activesupport/lib/active_support/core_ext/array/access.rb @@ -1,40 +1,48 @@ class Array # Returns the tail of the array from +position+. # - # %w( a b c d ).from(0) # => %w( a b c d ) - # %w( a b c d ).from(2) # => %w( c d ) - # %w( a b c d ).from(10) # => %w() - # %w().from(0) # => %w() + # %w( a b c d ).from(0) # => ["a", "b", "c", "d"] + # %w( a b c d ).from(2) # => ["c", "d"] + # %w( a b c d ).from(10) # => [] + # %w().from(0) # => [] def from(position) self[position, length] || [] end # Returns the beginning of the array up to +position+. # - # %w( a b c d ).to(0) # => %w( a ) - # %w( a b c d ).to(2) # => %w( a b c ) - # %w( a b c d ).to(10) # => %w( a b c d ) - # %w().to(0) # => %w() + # %w( a b c d ).to(0) # => ["a"] + # %w( a b c d ).to(2) # => ["a", "b", "c"] + # %w( a b c d ).to(10) # => ["a", "b", "c", "d"] + # %w().to(0) # => [] def to(position) first position + 1 end # Equal to self[1]. + # + # %w( a b c d e).second # => "b" def second self[1] end # Equal to self[2]. + # + # %w( a b c d e).third # => "c" def third self[2] end # Equal to self[3]. + # + # %w( a b c d e).fourth # => "d" def fourth self[3] end # Equal to self[4]. + # + # %w( a b c d e).fifth # => "e" def fifth self[4] end -- cgit v1.2.3 From 1e1d1da3ad71ed43fcff73bc3956579b69a1563d Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Mon, 28 May 2012 01:23:38 -0500 Subject: add examples to Array#to_sentence [ci skip] --- .../active_support/core_ext/array/conversions.rb | 28 ++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index 24aa28b895..d794713ed5 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -4,10 +4,30 @@ require 'active_support/core_ext/hash/reverse_merge' require 'active_support/core_ext/string/inflections' class Array - # Converts the array to a comma-separated sentence where the last element is joined by the connector word. Options: - # * :words_connector - The sign or word used to join the elements in arrays with two or more elements (default: ", ") - # * :two_words_connector - The sign or word used to join the elements in arrays with two elements (default: " and ") - # * :last_word_connector - The sign or word used to join the last element in arrays with three or more elements (default: ", and ") + # Converts the array to a comma-separated sentence where the last element is + # joined by the connector word. + # + # Options: + # + # * :words_connector - The sign or word used to join the elements + # in arrays with two or more elements (default: ", "). + # * :two_words_connector - The sign or word used to join the elements + # in arrays with two elements (default: " and "). + # * :last_word_connector - The sign or word used to join the last element + # in arrays with three or more elements (default: ", and "). + # + # Examples: + # + # [].to_sentence # => "" + # ['one'].to_sentence # => "one" + # ['one', 'two'].to_sentence # => "one and two" + # ['one', 'two', 'three'].to_sentence # => "one, two, and three" + # + # ['one', 'two'].to_sentence(two_words_connector: '-') + # # => "one-two" + # + # ['one', 'two', 'three'].to_sentence(words_connector: ' or ', last_word_connector: ' or at least ') + # # => "one or two or at least three" def to_sentence(options = {}) options.assert_valid_keys(:words_connector, :two_words_connector, :last_word_connector, :locale) -- cgit v1.2.3 From c6219b021574c4da69fbe0687726763fced9e492 Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Mon, 28 May 2012 01:24:34 -0500 Subject: add :locale option to Array#to_sentence documentation [ci skip] --- .../active_support/core_ext/array/conversions.rb | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index d794713ed5..74b5c73664 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -7,6 +7,10 @@ class Array # Converts the array to a comma-separated sentence where the last element is # joined by the connector word. # + # You can pass the following options to change the default behaviour. If you + # pass an option key that doesn't exist in the next list, it will raise an + # "Unknow key" error. + # # Options: # # * :words_connector - The sign or word used to join the elements @@ -15,6 +19,9 @@ class Array # in arrays with two elements (default: " and "). # * :last_word_connector - The sign or word used to join the last element # in arrays with three or more elements (default: ", and "). + # * :locale - If +i18n+ is available, you can set a locale and use + # the connector options defined on the 'support.array' namespace in the + # corresponding dictionary file. # # Examples: # @@ -23,11 +30,31 @@ class Array # ['one', 'two'].to_sentence # => "one and two" # ['one', 'two', 'three'].to_sentence # => "one, two, and three" # + # ['one', 'two'].to_sentence(passing: 'invalid option') + # # => ArgumentError: Unknown key :passing + # # ['one', 'two'].to_sentence(two_words_connector: '-') # # => "one-two" # # ['one', 'two', 'three'].to_sentence(words_connector: ' or ', last_word_connector: ' or at least ') # # => "one or two or at least three" + # + # Examples using :locale option: + # + # # With the next locale dictionary: + # #  + # # es: + # # support: + # # array: + # # words_connector: " o " + # # two_words_connector: " y " + # # last_word_connector: " o al menos " + # + # ['uno', 'dos'].to_sentence(locale: :es) + # # => "uno y dos" + # + # ['uno', 'dos', 'tres'].to_sentence(locale: :es) + # # => "uno o dos o al menos tres" def to_sentence(options = {}) options.assert_valid_keys(:words_connector, :two_words_connector, :last_word_connector, :locale) -- cgit v1.2.3 From 0ad2146ccf45b3a26924e729a92cd2ff98356413 Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Mon, 28 May 2012 02:18:35 -0500 Subject: remove :nodoc: from Class#subclasses [ci skip] --- .../lib/active_support/core_ext/class/subclasses.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/class/subclasses.rb b/activesupport/lib/active_support/core_ext/class/subclasses.rb index 74ea047c24..c2e0ebb3d4 100644 --- a/activesupport/lib/active_support/core_ext/class/subclasses.rb +++ b/activesupport/lib/active_support/core_ext/class/subclasses.rb @@ -1,11 +1,11 @@ require 'active_support/core_ext/module/anonymous' require 'active_support/core_ext/module/reachable' -class Class #:nodoc: +class Class begin ObjectSpace.each_object(Class.new) {} - def descendants + def descendants # :nodoc: descendants = [] ObjectSpace.each_object(singleton_class) do |k| descendants.unshift k unless k == self @@ -13,7 +13,7 @@ class Class #:nodoc: descendants end rescue StandardError # JRuby - def descendants + def descendants # :nodoc: descendants = [] ObjectSpace.each_object(Class) do |k| descendants.unshift k if k < self @@ -25,7 +25,13 @@ class Class #:nodoc: # Returns an array with the direct children of +self+. # - # Integer.subclasses # => [Bignum, Fixnum] + # Integer.subclasses # => [Fixnum, Bignum] + # + # class Foo; end + # class Bar < Foo; end + # class Baz < Foo; end + # + # Foo.subclasses # => [Baz, Bar] def subclasses subclasses, chain = [], descendants chain.each do |k| -- cgit v1.2.3 From 422d3a4f042df8ea4bd42b0dca38a295a218c376 Mon Sep 17 00:00:00 2001 From: Alvaro Pereyra Date: Mon, 28 May 2012 02:25:05 -0500 Subject: Updates documentation with cleaner examples and texts [ci skip] --- .../lib/active_support/core_ext/array/conversions.rb | 15 ++++++++++++++- .../active_support/core_ext/big_decimal/conversions.rb | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index 193388a158..d5735dd7eb 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -39,7 +39,19 @@ class Array end # Converts a collection of elements into a formatted string by calling - # to_s on all elements and joining them: + # to_s on all elements and joining them. Having this model: + # + # + # class Blog < ActiveRecord::Base + # def to_s + # title + # end + # end + # + # Blog.all.map(&:title) #=> ["First Post", "Second Post", "Third post"] + # + # + # to_formatted_s shows us: # # Blog.all.to_formatted_s # => "First PostSecond PostThird Post" # @@ -47,6 +59,7 @@ class Array # id list: # # Blog.all.to_formatted_s(:db) # => "1,2,3" + def to_formatted_s(format = :default) case format when :db diff --git a/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb b/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb index 3ec7e576c8..5b673e104a 100644 --- a/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb +++ b/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb @@ -4,6 +4,7 @@ require 'yaml' class BigDecimal YAML_MAPPING = { 'Infinity' => '.Inf', '-Infinity' => '-.Inf', 'NaN' => '.NaN' } + def encode_with(coder) string = to_s coder.represent_scalar(nil, YAML_MAPPING[string] || string) -- cgit v1.2.3 From a42bc4c9dfd7da50604d4136c9466dc7fcd3da14 Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Mon, 28 May 2012 02:40:26 -0500 Subject: fix empty lines [ci skip] --- activesupport/lib/active_support/core_ext/array/conversions.rb | 1 - activesupport/lib/active_support/core_ext/big_decimal/conversions.rb | 1 - 2 files changed, 2 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index 2af87d3b6b..d8b8585ada 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -106,7 +106,6 @@ class Array # id list: # # Blog.all.to_formatted_s(:db) # => "1,2,3" - def to_formatted_s(format = :default) case format when :db diff --git a/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb b/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb index 5b673e104a..3ec7e576c8 100644 --- a/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb +++ b/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb @@ -4,7 +4,6 @@ require 'yaml' class BigDecimal YAML_MAPPING = { 'Infinity' => '.Inf', '-Infinity' => '-.Inf', 'NaN' => '.NaN' } - def encode_with(coder) string = to_s coder.represent_scalar(nil, YAML_MAPPING[string] || string) -- cgit v1.2.3 From 95fde2c09b1261de84828d19cf9d895120c5b543 Mon Sep 17 00:00:00 2001 From: Alvaro Pereyra Date: Mon, 28 May 2012 03:24:52 -0500 Subject: Updates Array conversions method documentations and clean ups --- activesupport/lib/active_support/core_ext/array/conversions.rb | 4 ---- 1 file changed, 4 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index d8b8585ada..2b7e9942bf 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -23,8 +23,6 @@ class Array # the connector options defined on the 'support.array' namespace in the # corresponding dictionary file. # - # Examples: - # # [].to_sentence # => "" # ['one'].to_sentence # => "one" # ['one', 'two'].to_sentence # => "one and two" @@ -88,7 +86,6 @@ class Array # Converts a collection of elements into a formatted string by calling # to_s on all elements and joining them. Having this model: # - # # class Blog < ActiveRecord::Base # def to_s # title @@ -97,7 +94,6 @@ class Array # # Blog.all.map(&:title) #=> ["First Post", "Second Post", "Third post"] # - # # to_formatted_s shows us: # # Blog.all.to_formatted_s # => "First PostSecond PostThird Post" -- cgit v1.2.3 From 5487daba9736c70b3e2ae868f3e92b24f6620273 Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Tue, 29 May 2012 00:00:32 -0500 Subject: fix example format and add markup in Module#mattr_accessor documentation [ci skip] --- .../core_ext/module/attribute_accessors.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb index b8cb2e347f..672cc0256f 100644 --- a/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb @@ -46,19 +46,19 @@ class Module # Extends the module object with module and instance accessors for class attributes, # just like the native attr* accessors for instance attributes. # - # module AppConfiguration - # mattr_accessor :google_api_key + # module AppConfiguration + # mattr_accessor :google_api_key # - # self.google_api_key = "123456789" - # end + # self.google_api_key = "123456789" + # end # - # AppConfiguration.google_api_key # => "123456789" - # AppConfiguration.google_api_key = "overriding the api key!" - # AppConfiguration.google_api_key # => "overriding the api key!" + # AppConfiguration.google_api_key # => "123456789" + # AppConfiguration.google_api_key = "overriding the api key!" + # AppConfiguration.google_api_key # => "overriding the api key!" # - # To opt out of the instance writer method, pass instance_writer: false. - # To opt out of the instance reader method, pass instance_reader: false. - # To opt out of both instance methods, pass instance_accessor: false. + # To opt out of the instance writer method, pass instance_writer: false. + # To opt out of the instance reader method, pass instance_reader: false. + # To opt out of both instance methods, pass instance_accessor: false. def mattr_accessor(*syms) mattr_reader(*syms) mattr_writer(*syms) -- cgit v1.2.3 From e591b7b4e1825f34906664eb27aabd9aa6cc56b8 Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Wed, 30 May 2012 22:28:58 +0530 Subject: some copy edits [ci skip] --- activesupport/lib/active_support/core_ext/array/conversions.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index 2b7e9942bf..1e0de651c7 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -8,8 +8,8 @@ class Array # joined by the connector word. # # You can pass the following options to change the default behaviour. If you - # pass an option key that doesn't exist in the next list, it will raise an - # "Unknow key" error. + # pass an option key that doesn't exist in the list below, it will raise an + # ArgumentError. # # Options: # @@ -39,7 +39,7 @@ class Array # # Examples using :locale option: # - # # With the next locale dictionary: + # # Given this locale dictionary: # #  # # es: # # support: @@ -88,7 +88,7 @@ class Array # # class Blog < ActiveRecord::Base # def to_s - # title + # title # end # end # -- cgit v1.2.3