On Jun 2, 2007, at 10:07 AM, Lloyd L. wrote:
but then we need to check for length errors as mentioned before.
and have done with it. How do we make it readable? How do we let
choose
which is the more “ruby like” in its approach? How can I tell which
approach is ugly and which is elegant?
–
Posted via http://www.ruby-forum.com/.
Well, this is how Rails solves this in ActiveSupport
vendor/rails/activesupport/lib/active_support/core_ext/string/access.rb
module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module String #:nodoc:
# Makes it easier to access parts of a string, such as
specific characters and substrings.
module Access
#snip…
# Returns the last character of the string or the last +limit
- characters.
#
# Examples:
# “hello”.last # => “o”
# “hello”.last(2) # => “lo”
# “hello”.last(10) # => “hello”
def last(limit = 1)
(chars[(-limit)…-1] || self).to_s
end
end
end
end
end
Which is mixed into String by:
vendor/rails/activesupport/lib/active_support/core_ext/string.rb
require File.dirname(FILE) + ‘/string/inflections’
require File.dirname(FILE) + ‘/string/conversions’
require File.dirname(FILE) + ‘/string/access’
require File.dirname(FILE) + ‘/string/starts_ends_with’
require File.dirname(FILE) + ‘/string/iterators’
require File.dirname(FILE) + ‘/string/unicode’
class String #:nodoc:
include ActiveSupport::CoreExtensions::String::Access
include ActiveSupport::CoreExtensions::String::Conversions
include ActiveSupport::CoreExtensions::String::Inflections
include ActiveSupport::CoreExtensions::String::StartsEndsWith
include ActiveSupport::CoreExtensions::String::Iterators
include ActiveSupport::CoreExtensions::String::Unicode
end
So I’d venture to say that the Ruby Way is to simply open up the
String class and give it a new method. Since there is already an
Array#first that gives [1,2,3,4].first(2) => [1,2], it just seems
right to match that with .last and move the pair onto String treating
the individual characters like the elements of the Array.
Although the longer-term would seem to be to reconcile the []
behavior with Ranges:
“abcde”[0…3]
=> “abc”
“abcde”[-3…-1]
=> “cde”
“abcdefghijklmno”[-10…-1]
=> “fghijklmno”
“abcdefghijklmno”[0…10]
=> “abcdefghij”
“abcd”[0…10]
=> “abcd”
“abcd”[-10…-1]
=> nil
If only that were also “abcd”.
-Rob
Rob B. http://agileconsultingllc.com
[email protected]