From d77deb89d54b18c662ae3de103802e4d7a9d7d08 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Sun, 28 Dec 2008 13:21:10 +0300 Subject: Annotated metaprogramming code across ActiveSupport --- .../core_ext/class/attribute_accessors.rb | 46 ++++++++--------- .../core_ext/class/delegating_attributes.rb | 42 ++++++++-------- .../core_ext/class/inheritable_attributes.rb | 58 +++++++++++----------- .../core_ext/module/attribute_accessors.rb | 46 ++++++++--------- activesupport/lib/active_support/core_ext/proc.rb | 6 +-- .../active_support/core_ext/string/multibyte.rb | 6 ++- 6 files changed, 104 insertions(+), 100 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb index 186ca69c05..6b2ac8b38b 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb @@ -10,18 +10,18 @@ class Class def cattr_reader(*syms) syms.flatten.each do |sym| next if sym.is_a?(Hash) - class_eval(<<-EOS, __FILE__, __LINE__) - unless defined? @@#{sym} - @@#{sym} = nil - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + unless defined? @@#{sym} # unless defined @@property + @@#{sym} = nil # @@property = nil + end # end - def self.#{sym} - @@#{sym} - end + def self.#{sym} # def self.property + @@#{sym} # @@property + end # end - def #{sym} - @@#{sym} - end + def #{sym} # def property + @@#{sym} # @@property + end # end EOS end end @@ -29,19 +29,19 @@ class Class def cattr_writer(*syms) options = syms.extract_options! syms.flatten.each do |sym| - class_eval(<<-EOS, __FILE__, __LINE__) - unless defined? @@#{sym} - @@#{sym} = nil - end - - def self.#{sym}=(obj) - @@#{sym} = obj - end - - #{" - def #{sym}=(obj) - @@#{sym} = obj - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + unless defined? @@#{sym} # unless defined? @@property + @@#{sym} = nil # @@property = nil + end # end + + def self.#{sym}=(obj) # def self.property=(obj) + @@#{sym} = obj # @@property + end # end + + #{" + def #{sym}=(obj) # def property=(obj) + @@#{sym} = obj # @@property = obj + end # end " unless options[:instance_writer] == false } EOS end diff --git a/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb b/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb index 368317df9b..3b093a9a65 100644 --- a/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb +++ b/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb @@ -8,33 +8,33 @@ class Class def superclass_delegating_reader(*names) class_name_to_stop_searching_on = self.superclass.name.blank? ? "Object" : self.superclass.name names.each do |name| - class_eval <<-EOS - def self.#{name} - if defined?(@#{name}) - @#{name} - elsif superclass < #{class_name_to_stop_searching_on} && superclass.respond_to?(:#{name}) - superclass.#{name} - end - end - def #{name} - self.class.#{name} - end - def self.#{name}? - !!#{name} - end - def #{name}? - !!#{name} - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + def self.#{name} # def self.property + if defined?(@#{name}) # if defined?(@property) + @#{name} # @property + elsif superclass < #{class_name_to_stop_searching_on} && superclass.respond_to?(:#{name}) # elseif superclass < Object && superclass.respond_to?(:property) + superclass.#{name} # superclass.property + end # end + end # end + def #{name} # def property + self.class.#{name} # self.class.property + end # end + def self.#{name}? # def self.property? + !!#{name} # !!property + end # end + def #{name}? # def property? + !!#{name} # !!property + end # end EOS end end def superclass_delegating_writer(*names) names.each do |name| - class_eval <<-EOS - def self.#{name}=(value) - @#{name} = value - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + def self.#{name}=(value) # def self.property=(value) + @#{name} = value # @property = value + end # end EOS end end diff --git a/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb index e6143a274b..8d0233ce9a 100644 --- a/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb +++ b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb @@ -10,14 +10,14 @@ class Class # :nodoc: def class_inheritable_reader(*syms) syms.each do |sym| next if sym.is_a?(Hash) - class_eval <<-EOS - def self.#{sym} - read_inheritable_attribute(:#{sym}) - end - - def #{sym} - self.class.#{sym} - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + def self.#{sym} # def self.after_add + read_inheritable_attribute(:#{sym}) # read_inheritable_attribute(:after_add) + end # end + + def #{sym} # def after_add + self.class.#{sym} # self.class.after_add + end # end EOS end end @@ -25,15 +25,15 @@ class Class # :nodoc: def class_inheritable_writer(*syms) options = syms.extract_options! syms.each do |sym| - class_eval <<-EOS - def self.#{sym}=(obj) - write_inheritable_attribute(:#{sym}, obj) - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + def self.#{sym}=(obj) # def self.property=(obj) + write_inheritable_attribute(:#{sym}, obj) # write_inheritable_attribute(:property, obj) + end # end #{" - def #{sym}=(obj) - self.class.#{sym} = obj - end + def #{sym}=(obj) # def property=(obj) + self.class.#{sym} = obj # self.class.property = obj + end # end " unless options[:instance_writer] == false } EOS end @@ -42,15 +42,15 @@ class Class # :nodoc: def class_inheritable_array_writer(*syms) options = syms.extract_options! syms.each do |sym| - class_eval <<-EOS - def self.#{sym}=(obj) - write_inheritable_array(:#{sym}, obj) - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + def self.#{sym}=(obj) # def self.property=(obj) + write_inheritable_array(:#{sym}, obj) # write_inheritable_array(:property, obj) + end # end #{" - def #{sym}=(obj) - self.class.#{sym} = obj - end + def #{sym}=(obj) # def property=(obj) + self.class.#{sym} = obj # self.class.property = obj + end # end " unless options[:instance_writer] == false } EOS end @@ -59,15 +59,15 @@ class Class # :nodoc: def class_inheritable_hash_writer(*syms) options = syms.extract_options! syms.each do |sym| - class_eval <<-EOS - def self.#{sym}=(obj) - write_inheritable_hash(:#{sym}, obj) - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + def self.#{sym}=(obj) # def self.property=(obj) + write_inheritable_hash(:#{sym}, obj) # write_inheritable_hash(:property, obj) + end # end #{" - def #{sym}=(obj) - self.class.#{sym} = obj - end + def #{sym}=(obj) # def property=(obj) + self.class.#{sym} = obj # self.class.property = obj + end # end " unless options[:instance_writer] == false } EOS end 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 51e1c9af90..9467eb71ac 100644 --- a/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb @@ -14,18 +14,18 @@ class Module def mattr_reader(*syms) syms.each do |sym| next if sym.is_a?(Hash) - class_eval(<<-EOS, __FILE__, __LINE__) - unless defined? @@#{sym} - @@#{sym} = nil - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + unless defined? @@#{sym} # unless defined? @@property + @@#{sym} = nil # @@ property = nil + end # end - def self.#{sym} - @@#{sym} - end + def self.#{sym} # def self.property + @@#{sym} # @@property + end # end - def #{sym} - @@#{sym} - end + def #{sym} # def property + @@#{sym} # @@property + end # end EOS end end @@ -33,19 +33,19 @@ class Module def mattr_writer(*syms) options = syms.extract_options! syms.each do |sym| - class_eval(<<-EOS, __FILE__, __LINE__) - unless defined? @@#{sym} - @@#{sym} = nil - end - - def self.#{sym}=(obj) - @@#{sym} = obj - end - - #{" - def #{sym}=(obj) - @@#{sym} = obj - end + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + unless defined? @@#{sym} # unless defined? @@property + @@#{sym} = nil # @@ property = nil + end # end + + def self.#{sym}=(obj) # def self.property=(obj) + @@#{sym} = obj # @@property = obj + end # end + + #{" + def #{sym}=(obj) # def property=(obj) + @@#{sym} = obj # @@property = obj + end # end " unless options[:instance_writer] == false } EOS end diff --git a/activesupport/lib/active_support/core_ext/proc.rb b/activesupport/lib/active_support/core_ext/proc.rb index 2ca23f62ef..5c29cc32a2 100644 --- a/activesupport/lib/active_support/core_ext/proc.rb +++ b/activesupport/lib/active_support/core_ext/proc.rb @@ -3,9 +3,9 @@ class Proc #:nodoc: block, time = self, Time.now (class << object; self end).class_eval do method_name = "__bind_#{time.to_i}_#{time.usec}" - define_method(method_name, &block) - method = instance_method(method_name) - remove_method(method_name) + define_method(method_name, &block) # define_method("__bind_1230458026_720454", &block) + method = instance_method(method_name) # method = instance_method("__bind_1230458026_720454") + remove_method(method_name) # remove_method("__bind_1230458026_720454") method end.bind(object) end diff --git a/activesupport/lib/active_support/core_ext/string/multibyte.rb b/activesupport/lib/active_support/core_ext/string/multibyte.rb index a4caa83b74..8f8f0968fd 100644 --- a/activesupport/lib/active_support/core_ext/string/multibyte.rb +++ b/activesupport/lib/active_support/core_ext/string/multibyte.rb @@ -55,7 +55,11 @@ module ActiveSupport #:nodoc: unless '1.8.7 and later'.respond_to?(:chars) def chars - ActiveSupport::Deprecation.warn('String#chars has been deprecated in favor of String#mb_chars.', caller) + # FIXME: + # ActiveSupport::Deprecation refers to RAILS_ENV + # and is a show stopper for 3rd party applications + # that only want ActiveSupport + ActiveSupport::Deprecation.warn('String#chars has been deprecated in favor of String#mb_chars.', caller) if defined?(ActiveSupport::Deprecation) mb_chars end end -- cgit v1.2.3 From c2d23affad0ed4542e3906c334a7b27b07fc695c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 7 Jan 2009 13:19:48 -0800 Subject: Object#tap for Ruby < 1.8.7 --- activesupport/lib/active_support/core_ext/object/misc.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/object/misc.rb b/activesupport/lib/active_support/core_ext/object/misc.rb index 46f9c7d676..4570570bbc 100644 --- a/activesupport/lib/active_support/core_ext/object/misc.rb +++ b/activesupport/lib/active_support/core_ext/object/misc.rb @@ -40,6 +40,21 @@ class Object value end + # Yields x to the block, and then returns x. + # The primary purpose of this method is to "tap into" a method chain, + # in order to perform operations on intermediate results within the chain. + # + # (1..10).tap { |x| puts "original: #{x.inspect}" }.to_a. + # tap { |x| puts "array: #{x.inspect}" }. + # select { |x| x%2 == 0 }. + # tap { |x| puts "evens: #{x.inspect}" }. + # map { |x| x*x }. + # tap { |x| puts "squares: #{x.inspect}" } + def tap + yield self + self + end unless Object.respond_to?(:tap) + # An elegant way to factor duplication out of options passed to a series of # method calls. Each method called in the block, with the block variable as # the receiver, will have its options merged with the default +options+ hash -- cgit v1.2.3 From b1f078bddfecd40cce47b7db738620f2df2219c9 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Tue, 24 Feb 2009 17:25:21 -0800 Subject: First, very early, AbstractController code. More to come --- .../core_ext/class/inheritable_attributes.rb | 79 ++++++++++++++++++++++ 1 file changed, 79 insertions(+) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb index 70fdde3a58..c121933050 100644 --- a/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb +++ b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb @@ -138,3 +138,82 @@ class Class # :nodoc: alias inherited_without_inheritable_attributes inherited alias inherited inherited_with_inheritable_attributes end + +class Class + # Defines class-level inheritable attribute reader. Attributes are available to subclasses, + # each subclass has a copy of parent's attribute. + # + # @param *syms Array of attributes to define inheritable reader for. + # @return Array of attributes converted into inheritable_readers. + # + # @api public + # + # @todo Do we want to block instance_reader via :instance_reader => false + # @todo It would be preferable that we do something with a Hash passed in + # (error out or do the same as other methods above) instead of silently + # moving on). In particular, this makes the return value of this function + # less useful. + def extlib_inheritable_reader(*ivars) + instance_reader = ivars.pop[:reader] if ivars.last.is_a?(Hash) + + ivars.each do |ivar| + self.class_eval <<-RUBY, __FILE__, __LINE__ + 1 + def self.#{ivar} + return @#{ivar} if self.object_id == #{self.object_id} || defined?(@#{ivar}) + ivar = superclass.#{ivar} + return nil if ivar.nil? && !#{self}.instance_variable_defined?("@#{ivar}") + @#{ivar} = ivar && !ivar.is_a?(Module) && !ivar.is_a?(Numeric) && !ivar.is_a?(TrueClass) && !ivar.is_a?(FalseClass) ? ivar.dup : ivar + end + RUBY + unless instance_reader == false + self.class_eval <<-RUBY, __FILE__, __LINE__ + 1 + def #{ivar} + self.class.#{ivar} + end + RUBY + end + end + end + + # Defines class-level inheritable attribute writer. Attributes are available to subclasses, + # each subclass has a copy of parent's attribute. + # + # @param *syms Boolean}]> Array of attributes to + # define inheritable writer for. + # @option syms :instance_writer if true, instance-level inheritable attribute writer is defined. + # @return An Array of the attributes that were made into inheritable writers. + # + # @api public + # + # @todo We need a style for class_eval <<-HEREDOC. I'd like to make it + # class_eval(<<-RUBY, __FILE__, __LINE__), but we should codify it somewhere. + def extlib_inheritable_writer(*ivars) + instance_writer = ivars.pop[:instance_writer] if ivars.last.is_a?(Hash) + ivars.each do |ivar| + self.class_eval <<-RUBY, __FILE__, __LINE__ + 1 + def self.#{ivar}=(obj) + @#{ivar} = obj + end + RUBY + unless instance_writer == false + self.class_eval <<-RUBY, __FILE__, __LINE__ + 1 + def #{ivar}=(obj) self.class.#{ivar} = obj end + RUBY + end + end + end + + # Defines class-level inheritable attribute accessor. Attributes are available to subclasses, + # each subclass has a copy of parent's attribute. + # + # @param *syms Boolean}]> Array of attributes to + # define inheritable accessor for. + # @option syms :instance_writer if true, instance-level inheritable attribute writer is defined. + # @return An Array of attributes turned into inheritable accessors. + # + # @api public + def extlib_inheritable_accessor(*syms) + class_inheritable_reader(*syms) + class_inheritable_writer(*syms) + end +end \ No newline at end of file -- cgit v1.2.3 From 6001cea5d70344d4c13b5cff94ee853f5f5462ce Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Tue, 3 Mar 2009 16:42:20 -0800 Subject: Helpers with an initial test --- .../lib/active_support/core_ext/class/inheritable_attributes.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb index c121933050..2f18666ab9 100644 --- a/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb +++ b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb @@ -188,7 +188,7 @@ class Class # @todo We need a style for class_eval <<-HEREDOC. I'd like to make it # class_eval(<<-RUBY, __FILE__, __LINE__), but we should codify it somewhere. def extlib_inheritable_writer(*ivars) - instance_writer = ivars.pop[:instance_writer] if ivars.last.is_a?(Hash) + instance_writer = ivars.pop[:writer] if ivars.last.is_a?(Hash) ivars.each do |ivar| self.class_eval <<-RUBY, __FILE__, __LINE__ + 1 def self.#{ivar}=(obj) @@ -213,7 +213,7 @@ class Class # # @api public def extlib_inheritable_accessor(*syms) - class_inheritable_reader(*syms) - class_inheritable_writer(*syms) + extlib_inheritable_reader(*syms) + extlib_inheritable_writer(*syms) end end \ No newline at end of file -- cgit v1.2.3 From 5a8b481f717470b952ac7eb890f260ea98428153 Mon Sep 17 00:00:00 2001 From: Michael Curtis Date: Tue, 10 Mar 2009 12:14:54 -0500 Subject: Time.local instances: Adding 24.hours across the DST boundary adds 24 hours instead of one day [#2066 state:resolved] --- .../lib/active_support/core_ext/time/calculations.rb | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 5ed750afcc..d13d0e01a7 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -116,22 +116,14 @@ module ActiveSupport #:nodoc: seconds_to_advance == 0 ? time_advanced_by_date : time_advanced_by_date.since(seconds_to_advance) end - # Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension + # Returns a new Time representing the time a number of seconds ago def ago(seconds) self.since(-seconds) end - # Returns a new Time representing the time a number of seconds since the instance time, this is basically a wrapper around - # the Numeric extension. + # Returns a new Time representing the time a number of seconds since the instance time def since(seconds) - f = seconds.since(self) - if ActiveSupport::Duration === seconds - f - else - initial_dst = self.dst? ? 1 : 0 - final_dst = f.dst? ? 1 : 0 - (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f - end + self + seconds rescue self.to_datetime.since(seconds) end -- cgit v1.2.3 From 7685ea20b4915f907082a22028260d04024a200b Mon Sep 17 00:00:00 2001 From: Geoff Buesing Date: Sun, 5 Apr 2009 10:15:54 -0500 Subject: Hash::XML_TYPE_NAMES: no longer a need for a TimeWithZone entry; this class will now match "Time" --- activesupport/lib/active_support/core_ext/hash/conversions.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb index 10435975c5..f8a5e70eea 100644 --- a/activesupport/lib/active_support/core_ext/hash/conversions.rb +++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb @@ -28,8 +28,7 @@ module ActiveSupport #:nodoc: "FalseClass" => "boolean", "Date" => "date", "DateTime" => "datetime", - "Time" => "datetime", - "ActiveSupport::TimeWithZone" => "datetime" + "Time" => "datetime" } unless defined?(XML_TYPE_NAMES) XML_FORMATTING = { -- cgit v1.2.3 From c1aa5b0e14cd4bd27a5d8bd85cf7c36fa5911830 Mon Sep 17 00:00:00 2001 From: Yehuda Katz and Carl Lerche Date: Tue, 7 Apr 2009 14:57:18 -0700 Subject: Add depends_on, use, and setup to abstract up ideas about module inheritance. --- .../lib/active_support/core_ext/module.rb | 1 + .../lib/active_support/core_ext/module/setup.rb | 26 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 activesupport/lib/active_support/core_ext/module/setup.rb (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/module.rb b/activesupport/lib/active_support/core_ext/module.rb index da8d28ec13..cb31437094 100644 --- a/activesupport/lib/active_support/core_ext/module.rb +++ b/activesupport/lib/active_support/core_ext/module.rb @@ -8,6 +8,7 @@ require 'active_support/core_ext/module/loading' require 'active_support/core_ext/module/aliasing' require 'active_support/core_ext/module/model_naming' require 'active_support/core_ext/module/synchronization' +require 'active_support/core_ext/module/setup' module ActiveSupport module CoreExtensions diff --git a/activesupport/lib/active_support/core_ext/module/setup.rb b/activesupport/lib/active_support/core_ext/module/setup.rb new file mode 100644 index 0000000000..e6dfd0cf56 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/module/setup.rb @@ -0,0 +1,26 @@ +class Module + attr_accessor :_setup_block + attr_accessor :_dependencies + + def setup(&blk) + @_setup_block = blk + end + + def use(mod) + return if self < mod + + (mod._dependencies || []).each do |dep| + use dep + end + # raise "Circular dependencies" if self < mod + include mod + extend mod.const_get("ClassMethods") if mod.const_defined?("ClassMethods") + class_eval(&mod._setup_block) if mod._setup_block + end + + def depends_on(mod) + return if self < mod + @_dependencies ||= [] + @_dependencies << mod + end +end \ No newline at end of file -- cgit v1.2.3 From 60896ca6f4c89260cb9770487f80dec829674b89 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 17 Apr 2009 13:44:59 -0500 Subject: Clearer String#first and #last edge cases. Fix that foo.first(0) == instead of foo. --- .../lib/active_support/core_ext/string/access.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/string/access.rb b/activesupport/lib/active_support/core_ext/string/access.rb index 7fb21fa4dd..e067f930da 100644 --- a/activesupport/lib/active_support/core_ext/string/access.rb +++ b/activesupport/lib/active_support/core_ext/string/access.rb @@ -41,9 +41,15 @@ module ActiveSupport #:nodoc: # "hello".first(2) # => "he" # "hello".first(10) # => "hello" def first(limit = 1) - mb_chars[0..(limit - 1)].to_s + if limit == 0 + '' + elsif limit >= size + self + else + mb_chars[0...limit].to_s + end end - + # Returns the last character of the string or the last +limit+ characters. # # Examples: @@ -51,7 +57,13 @@ module ActiveSupport #:nodoc: # "hello".last(2) # => "lo" # "hello".last(10) # => "hello" def last(limit = 1) - (mb_chars[(-limit)..-1] || self).to_s + if limit == 0 + '' + elsif limit >= size + self + else + mb_chars[(-limit)..-1].to_s + end end end else -- cgit v1.2.3 From 164a94d0bc8c9124ab820506e5ad79496395c026 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 20 Apr 2009 00:40:15 -0700 Subject: Clearer String#first and #last edge cases. Fix that 'foo'.first(0) == 'foo' instead of '' --- .../lib/active_support/core_ext/string/access.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/string/access.rb b/activesupport/lib/active_support/core_ext/string/access.rb index e067f930da..e806b321f1 100644 --- a/activesupport/lib/active_support/core_ext/string/access.rb +++ b/activesupport/lib/active_support/core_ext/string/access.rb @@ -81,11 +81,23 @@ module ActiveSupport #:nodoc: end def first(limit = 1) - self[0..(limit - 1)] + if limit == 0 + '' + elsif limit >= size + self + else + to(limit - 1) + end end def last(limit = 1) - from(-limit) || self + if limit == 0 + '' + elsif limit >= size + self + else + from(-limit) + end end end end -- cgit v1.2.3