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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:43:13+00:00 2026-05-16T05:43:13+00:00

Here’s what I’m doing: offers = v.offers.sort { |a,b| a.expires <=> b.expires } This

  • 0

Here’s what I’m doing:

offers = v.offers.sort { |a,b| a.expires <=> b.expires }

This data is loaded via ActiveResource (so each set of instance attributes is defined by the data it contains). However, a recent change in the incoming data has made the “expires” attribute optional. Is there a class definition change that will cause the sort method to go grab a default value if the attribute is missing from an instance?

edit: @Nikita

It looks like it won’t be that simple:

o.expires == nil?
NoMethodError: undefined method `expires' for #<Offer:0x00000100d3faa8>

o.expires?
=> nil

so I tried:

offers.sort{|a,b|
if a.expires?
    b.expires? ? 0 : -1
else
    b.expires? ? 1 : a.expires <=> b.expires
end
}

NoMethodError: undefined method `expires' for #<Offer:0x00000100d3faa8>

I was hoping to be able to update the class definition with something like:

expires ||= ""

… but I don’t know if that’s possible. I don’t really follow how the sorting blocks work yet, though. I know I could just loop through the offers and assign the value, but it seems grossly inefficient.

update

offers.sort{|a,b|
if defined? a.expires == nil
    (defined? b.expires == nil) ? 0 : -1
else
    (defined? b.expires == nil) ? 1 : a.expires <=> b.expires
end
}

ArgumentError: comparison of Offer with Offer failed

from (irb):70:in `sort'
from (irb):70
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0.beta4/lib/rails/commands/console.rb:47:in `start'
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0.beta4/lib/rails/commands/console.rb:8:in `start'
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0.beta4/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

Hooray for verbosity ;p

  • 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-16T05:43:13+00:00Added an answer on May 16, 2026 at 5:43 am

    I don’t get what ‘class definition’ you want. Won’t just adapting sort block solve the problem?

    if a.expires == nil
        b.expires == nil ? 0 : -1
    else
        b.expires == nil ? 1 : a.expires <=> b.expires
    end
    

    on update
    I see. There’s a defined? operator in ruby which might help. It returns nil or string representing type of element (like ‘method’).

    if defined? a.expires == nil
        (defined? b.expires == nil) ? 0 : -1
    else
        (defined? b.expires == nil) ? 1 : a.expires <=> b.expires
    end
    

    I don’t really follow how the sorting blocks work yet, though.
    That’s easy. It should return -1, 0 or 1 depending on whether first element is smaller, equal or bigger than second.
    <=> operator does more or less the same thing:

    1 <=> 2 # result is -1
    2 <=> 2 # result is 0
    15 <=> 2 # result is 1
    

    update again
    Works for me

    class Offer
      def initialize(e)
        @expires = e
      end
    
      def expires
        @expires
      end
    end
    offers = [Offer.new(3), Offer.new(1), Offer.new(2), Object.new]
    
    
    offers.sort! { |a, b|
      if (defined? a.expires) == nil
        ((defined? b.expires) == nil) ? 0 : -1
      else
        ((defined? b.expires) == nil) ? 1 : a.expires <=> b.expires
      end
    }
    puts offers.inspect
    

    Your error indicates that a.expires has type Offer, which can’t be compared.

    Hooray for verbosity ;p
    Welcome to rails 🙂

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

Sidebar

Related Questions

Here's my situation: I have a Data Template set up which contains a ToggleButton
Here is some code I made :) @echo off set source=R:\Contracts\ set destination=R:\Contracts\Sites\ ROBOCOPY
Here is the situation, I am attempting to fire a set of Gallio tests
Here my code: $(document).ready(function() { $('#mid_select').live('click', function(e){ $('#middle').load( $(this).attr('href') + ' #middle'); var page
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is the css: #content ul { font-size: 12px; } I am trying this:
Here is a app contorller directory from Rails project doing a self study for
Here is what I am trying to achieve in PHP: I have this string:
Here is my simplified data structure: Object1.h template <class T> class Object1 { private:

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.