Skip to content

Object#presence should take a block - #13416

Closed
stephenprater wants to merge 1 commit into
rails:masterfrom
acumenbrands:presence_yield
Closed

stephenprater wants to merge 1 commit into
rails:masterfrom
acumenbrands:presence_yield

Conversation

@stephenprater

Copy link
Copy Markdown

In order to have parallel behavior with methods like #each, presence should
yield to a provided block if the object is present. This is
similar to an anaphoric if in Scheme, and is discussed at length here:

https://github.com/raganwald/homoiconic/blob/master/2012/05/anaphora.md

You can achieve similar functionality by appending try in its block
form to a presence call - but having presence evaluate the block itself
avoids the (confusing) call to try.

In order to have parallel behavior with methods like #each, presence should
yield to a provided block if the object is present.  This is
similar to an anaphoric if in Scheme, and is discussed at length here:

https://github.com/raganwald/homoiconic/blob/master/2012/05/anaphora.m

You can achieve similar functionality by appending `try` in its block
form to a presence call - but having presence evaluate the block itself
avoids the (confusing) call to try.
@carlosantoniodasilva

Copy link
Copy Markdown
Member

Can you showcase a real use case for this? I can't really think of anything right now (too late here already :D)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

use assert_nil

@stephenprater

Copy link
Copy Markdown
Author

The use case for this is basically the same as an anaphoric if or an assign in a conditional.

For an actual use case:

ordered_products = Product.long_and_complicated_query
if ordered_products.present?
  if sort.present?
    ordered_products = order_products.reorder( by: sort.order_by)
  else
    ordered_products = order_products.reorder
  end
end
ordered_products

# vs. 

Product.long_and_complicated_query.presence do |ordered_products|
  ordered_products.reorder by: sort.presence.order_by
end

That's not ACTUAL production code - but it's the gist of it.

@carlosantoniodasilva

Copy link
Copy Markdown
Member

I think sort.presence.order_by would raise if not present in that example, wouldn't it?

In any case, I think we could maybe write your code with something like this:

ordered_products = Product.long_and_complicated_query
ordered_products = order_products.reorder(by: sort.order_by) if sort.present?
ordered_products

In this example I believe you don't need to check for the presence of ordered_products, since that is a relation and would lead to load that relation. Sorry but I still don't see an improvement here.

Thanks!

@stephenprater

Copy link
Copy Markdown
Author

You're right - it would actually be sort.presence { sort.order_by }

As for the other point - you'd be right - but uhh... confession time.... remember when I said it was the "gist" of the production code? That's because that's not the ActiveRecord reorder method and long_and_complicated_query returns an Array not a relation. I understand that that's a terrible, terrible thing to do - but given that does the use case make any more sense?

@stephenprater

Copy link
Copy Markdown
Author

FTR I didn't write the terrible method - I'm just trying not to pile additional crap on top of it.

@avit

avit commented Jan 17, 2014

Copy link
Copy Markdown
Contributor

On the other hand, I could see an argument for the block being called when the value is not present, consistent with hash.fetch(:some_index) { |_, i| default_value_for(i) }.

  • When the value is present, you can just chain on it.
  • When the value is absent, you could pass a block for a default value, and then chain on it.

What is the purpose of having the block option for the first bullet point?

@stephenprater

Copy link
Copy Markdown
Author

Basically, calling the block is parallel to the behavior of each and other enumerable methods (and try for that matter) - where it's safe to call each on an empty array because they block will never be executed.

@avit

avit commented Jan 20, 2014

Copy link
Copy Markdown
Contributor

I found these for you, maybe relevant:

https://bugs.ruby-lang.org/issues/6373
https://bugs.ruby-lang.org/issues/6721

In any case, this is a feature request and such discussion happens on the rails-core mailing list. Rails Github issues are meant for bugs: you might get better traction with this question on the mailing list.

@rafaelfranca

Copy link
Copy Markdown
Member

I still not convinced we need this feature. I also agree with @avit that presence with block would cause confusion if it will run if present or not present.

That said I'm closing this one. Thank you for the contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants