Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6675339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:50:36+00:00 2026-05-26T03:50:36+00:00

So, thinking I’m all clever, I add a method like this to Object :

  • 0

So, thinking I’m all clever, I add a method like this to Object:

class Object
  def apply_if(cond)
    if cond
      yield self
    else
      return self
    end
  end
end

This (I thought) allows my to conditionally add bits to a method chain, which simplifies my ActiveRecord query manipulation quite a bit. But it gives a syntax error, which I can reduce down to the following code:

data = [1,2,3,4,5]
results = data.
  apply_if(true and false) do |q|
    q + [0,0]
  end

Likewise this errors:

results = data.apply_if(true and false){|q| q + [0,0]}

But this works:

results = data.apply_if ((true and false)) {|q| q + [0,0]}

As does:

results = data.apply_if (true && false) {|q| q + [0,0]}

I see that the differences there are all to do with operator precendence, but how can the precedence of an operator inside a pair of parentheses matter?

Why is there a syntax error here at all? I don’t see any likely syntactic ambiguities, and this method is identical in shape to the Array#reduce method.

I’ve tried a number of combinations here – explicit block parameters with calls, various types of explicit precedence inside the method definition. Using a lambda instead of a block worked fine, but is obviously too clunky to use for my purposes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:50:37+00:00Added an answer on May 26, 2026 at 3:50 am

    This has nothing to do with any of your code — the and operator just isn’t allowed in an argument list. As for why, you’d have to ask the Core team, but the most obvious possibility IMO is that its raison d’etre (having extremely low precedence) doesn’t work in an argument list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was thinking the other day, if someone is protecting their pages like this
Hi thinking i have an array like this : [0]=>[ [0]=>'Hey', [1]=>'H', [2]=>'He', ]
Thinking in regards to Sliverlight, I would like to know where would be good
Thinking that the answer to this is pretty obvious but here it goes: When
Without thinking about it at all I just want to say I should allow
While thinking about this question and conversing with the participants, the idea came up
Without thinking about it too much, I've been doing something like the following: 1)
After thinking about This Question and giving an answer to it I wanted to
Am thinking about a project which might use similar functionality to how Quick Add
Thinking I'd jump right into this without any issues, I was surprised that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.