Permalink
Browse files

Add String#inquiry as a convenience method for turning a string into …

…a StringInquirer object [DHH]
  • Loading branch information...
1 parent 389d15e commit 39372964d245ba7077515a698eaf71cb534800fe @dhh dhh committed Apr 18, 2011
View
@@ -1,5 +1,7 @@
*Rails 3.1.0 (unreleased)*
+* Add String#inquiry as a convenience method for turning a string into a StringInquirer object [DHH]
+
* Add Object#in? to test if an object is included in another object [Prem Sichanugrist, Brian Morearty, John Reitano]
* LocalCache strategy is now a real middleware class, not an anonymous class
@@ -11,3 +11,4 @@
require 'active_support/core_ext/string/exclude'
require 'active_support/core_ext/string/encoding'
require 'active_support/core_ext/string/strip'
+require 'active_support/core_ext/string/inquiry'
@@ -0,0 +1,13 @@
+require 'active_support/string_inquirer'
+
+class String
+ # Wraps the current string in the ActiveSupport::StringInquirer class,
+ # which gives you a prettier way to test for equality. Example:
+ #
+ # env = "production".inquiry
+ # env.production? # => true
+ # env.development? # => false
+ def inquiry
+ ActiveSupport::StringInquirer.new(self)
+ end
+end
@@ -250,6 +250,11 @@ def test_string_squish
# And changes the original string:
assert_equal original, expected
end
+
+ def test_string_inquiry
+ assert "production".inquiry.production?
+ assert !"production".inquiry.development?
@vijaydev

vijaydev Apr 18, 2011

Member

@dhh Will this not fail if the string has question marks or other non alpha-numeric characters?

@dhh

dhh Apr 18, 2011

Owner

Yes, so don't do that :). This is meant for status-type fields, not random strings.

+ end
def test_truncate
assert_equal "Hello World!", "Hello World!".truncate(12)

2 comments on commit 3937296

Owner

fxn replied Apr 18, 2011

Would be nice to patch the AS guide as well.

Member

vijaydev replied Apr 20, 2011

Hey, I've added this in the guides in docrails - rails/docrails@2f0dca5

Please sign in to comment.