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 8322299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:20:30+00:00 2026-06-08T23:20:30+00:00

Is there a way to make this situation more compact in rails views? Eg

  • 0

Is there a way to make this situation more compact in rails views?

Eg I have haml

= object.count unless object.count ==0

I sort of don’t like that has I’m repeating the function there, I would much rather have something like

= object.count unless ==0

Eg if I had more complex statements

= object.relations.where(attribute: "something").count unless zero?

I could split that into two lines say

- cnt = object.relations.where(attribute: "something").count
= cnt unless cnt==0

But for each situation I would have multiple lines, and storing a variable to use once sucks.

EDIT: just to elaborate I want to check if the number is 0, and if so not display anything. It looks nicer in the view that way.

UPDATE:

One of the answers made come up with a solution along these lines

class Object
  def unless
    self unless yield(self)
  end
end

So I can call whatever object I have with a block eg. .unless{|c| c<1}

This lets me tack the conditionals on, and keeps it pretty clear what is going on :), bonus is as it’s block driven I can use this on any object :P.

Thanks everyone 🙂

UPDATE EVEN MORE

Having |c| in the block sucked. So I looked up the api and changed it too

class Object
  def unless(&block)
    self unless instance_eval(&block)
  end
end

So now I can use .count.unless{zero?} to accomplish this :P. Or if I have a complicated condition I can add that in with |c| etc.

  • 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-06-08T23:20:31+00:00Added an answer on June 8, 2026 at 11:20 pm

    If this is only about count, you can monkey patch Enumerable:

    module Enumerable
      def count_or_empty_string
        self.any? ? self.count : ''
      end
    end
    

    If object is an enumerable, you can do this:

    = object.count_or_empty_string
    

    This will return an "" if object.count == 0 else it will return an integer. So there is no need for unless or if in your HAML anymore.

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

Sidebar

Related Questions

Is there any way to make code more compact for this situation? // pseudocode
Is there a way to make this code work? LogonControl.java @Audit(AuditType.LOGON) public void login(String
Is there a way to make this line shorter? bool pass = d !=
Is there any way to make this work in Java? public static void change(List<?
Is there a way to make this method generic so I can return a
Essentially, is there any way to make this code legal? main = print .
MyClass c = 10; Is there any way to make this code work? I
Is there any better way to make this query work? I'm looking for a
Is there a way to make a class such this one: class DB extends
Is there a way to make the UIPickerView read-only? This means that the user

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.