Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added Object#self which returns the object itself
  • Loading branch information
dhh committed Aug 1, 2014
1 parent 20405e5 commit 702ad71
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,3 +1,9 @@
* Added Object#self which returns the object itself. Useful when dealing with a chaining scenario, like Active Record scopes:

Event.public_send(state.presence_in?([ :trashed, :drafted ]) ? :self).order(:created_at)

*DHH*

* `Object#with_options` executes block in merging option context when
explicit receiver in not passed.

Expand Down
1 change: 1 addition & 0 deletions activesupport/lib/active_support/core_ext/object.rb
Expand Up @@ -2,6 +2,7 @@
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/object/duplicable'
require 'active_support/core_ext/object/deep_dup'
require 'active_support/core_ext/object/self'
require 'active_support/core_ext/object/try'
require 'active_support/core_ext/object/inclusion'

Expand Down
10 changes: 10 additions & 0 deletions activesupport/lib/active_support/core_ext/object/self.rb
@@ -0,0 +1,10 @@
class Object
# Returns the object itself. Useful when dealing with a chaining scenario, like Active Record scopes:
#
# Event.public_send(state.presence_in?([ :trashed, :drafted ]) ? :self).order(:created_at)
#
# @return Object
def self
self
end
end
9 changes: 9 additions & 0 deletions activesupport/test/core_ext/object/self_test.rb
@@ -0,0 +1,9 @@
require 'abstract_unit'
require 'active_support/core_ext/object'

class Object::SelfTest < ActiveSupport::TestCase
test 'self returns self' do
object = 'fun'
assert_equal object, object.self
end
end

3 comments on commit 702ad71

@rubiii
Copy link
Contributor

@rubiii rubiii commented on 702ad71 Aug 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example for Object#self seems incorrect, since the ternary is missing an else (:).

>> Event.public_send(state.presence_in?([ :trashed, :drafted ]) ? :self)
SyntaxError: (irb):1: syntax error, unexpected ')', expecting ':'

@dhh
Copy link
Member Author

@dhh dhh commented on 702ad71 Aug 2, 2014 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rubiii
Copy link
Contributor

@rubiii rubiii commented on 702ad71 Aug 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great. missed that one. thanks.

Please sign in to comment.