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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:01:44+00:00 2026-06-09T19:01:44+00:00

I am new to Ruby and Rails (switched from Python and Python frameworks). I’m

  • 0

I am new to Ruby and Rails (switched from Python and Python frameworks). I’m writing a simple dashboard website which displays information about the S.M.A.R.T. state of hard disks. Here I wrote a helper to display a badge in a table cell near the relevant S.M.A.R.T attribute if it’s value meets a condition. At first, the helper code was as simple as in Listing 1, but then I decided to draw a summary of all badges for the specific drive, in addition to the badges near individual S.M.A.R.T. attributes in the table. So at first I added a simple method like:

def smart_chk_device(attrs)
    attrs.each { |item| smart_chk_attr(item) }
end

But this approach didn’t work and it caused the entire array of attributes to be output to the resulting page. It only started to work when I made it as in Listing 2, but I believe there’s something wrong there, and the same thing can be done in a more simple way. Please show me the right, Ruby-way of doing it.

Listing 1:

module HomeHelper
    def smart_chk_attr(attr)
        case attr[:id].to_i
        when 1,197
            content_tag(:span, "Warning", :class => "label label-warning") if attr[:raw].to_i > 0
        when 5,7,10,196,198
            content_tag(:span, "Critical", :class => "label label-important") if attr[:raw].to_i > 0
        end
    end
end

Listing 2 (works, but I don’t like it):

module HomeHelper
    def smart_chk_attr(attr)
        case attr[:id].to_i
        when 1,197
            return content_tag(:span, "Warning", :class => "label label-warning") if attr[:raw].to_i > 0
        when 5,7,10,196,198
            return content_tag(:span, "Critical", :class => "label label-important") if attr[:raw].to_i > 0
        else
            return String.new
        end
        return String.new
    end

    def smart_chk_device(attrs)
        output = ""
        attrs.each { |item| output << smart_chk_attr(item) }
        return output.html_safe
    end
end

attrs is an Array of Hashes, where each Hash contains keys :id and :raw with the numeric code of a S.M.A.R.T attribute and its RAW value, both in Strings.

Also, RoR complaints if to remove the last “return String.new” in Listing 2. Why is it so? Doesn’t the “case” block all the possible cases, so that control should never reach the end of the function?

  • 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-09T19:01:46+00:00Added an answer on June 9, 2026 at 7:01 pm

    I believe this would behave in the same way, and is much shorter:

    module HomeHelper
    
      def smart_chk_attr(attr)
        return '' unless attr[:raw].to_i > 0
        case attr[:id].to_i
          when 1,197
            content_tag(:span, "Warning", :class => "label label-warning")
          when 5,7,10,196,198
            content_tag(:span, "Critical", :class => "label label-important")
          else ''
        end
      end
    
      def smart_chk_device(attrs)
        attrs.map { |item| smart_chk_attr(item) }.join.html_safe
      end
    
    end
    

    Ruby methods return the value of the last expression, so get rid of the explicit returns all over that method. Also, DRY: Don’t Repeat Yourself (the attr[:raw] check). In this case, I replaced those with a guard clause at the start of the method. Short-circuiting guard clauses are a matter of taste, but I like them and you’ll see them in a lot of Ruby code.

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

Sidebar

Related Questions

I'm new to ruby/rails and trying to build a simple Projects / Tags app
im new to ruby and rails. in RoR3 a controller inherits from the ActionController::Base
I'm new to Ruby and Rails and I have a simple controller that shows
I'm brand new to Ruby/Rails/RSpec, and hopefully this is a dumb/simple question. I'm currently
I am new to ruby on rails. I have a website with a lot
I am new to Ruby on Rails. I am trying to develop a website
I am new to Ruby on Rails I have a scenario in which I
I am new to Ruby and Rails . I have a requirement in which
I'm new in Ruby/Rails, so, my question: what is the perfect way to upload
I'm currently evaluating options for adding sub-domain support to a new Ruby on Rails

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.