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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:15:43+00:00 2026-05-28T07:15:43+00:00

I’m going through this tutorial: http://tutorials.jumpstartlab.com/projects/jsattend.html In iteration 7, step 3 we get to

  • 0

I’m going through this tutorial: http://tutorials.jumpstartlab.com/projects/jsattend.html

In iteration 7, step 3 we get to sort a hash, called state_data, which has nil as a key. The proposed solution is:

state_data = state_data.sort_by{|state, counter| state unless state.nil?}

Unfortunately, this isn’t working on ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.0.0]. For example:

~% irb
>> things = { nil => "a", 2 => "b", 3 => "c" }
>> things.sort_by { |k, v| k unless k.nil? }
ArgumentError: comparison of NilClass with 2 failed
    from (irb):6:in `sort_by'
    from (irb):6
    from /Users/jacopo/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'

The same goes with the equivalent:

>> things.sort_by { |k, v| k if k }
ArgumentError: comparison of NilClass with 2 failed
    from (irb):3:in `sort_by'
    from (irb):3
    from /Users/jacopo/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'

In the tutorial’s case, since it’s sorting on the state two letter code, a possible solution is:

state_data = state_data.sort_by{|state, counter| state.nil? ? "ZZ" : state }

which is clearly a hack.

What’s the Ruby-way to deal with this?

  • 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-28T07:15:43+00:00Added an answer on May 28, 2026 at 7:15 am

    The line

    state_data.sort_by { |state, counter| state unless state.nil? }
    

    is effectively equivalent to a simple state_data.sort, because (state unless state.nil?) == state in any case. What you could do is to seperate the proper keys from the nil keys and only sort the former:

    state_data.select(&:first).sort + state_data.reject(&:first)
    

    In the more general case, you can also define a custom comparison function like this:

    def compare(a, b)
      return a.object_id <=> b.object_id unless a || b
      return -1 unless b
      return 1  unless a
      a <=> b
    end
    
    state_data.sort { |a, b| compare(a.first, b.first) }
    

    By quickly looking at this, you see that it’s pretty ugly. In fact, you should rather rethink your choice of data structure here. nil keys don’t seem very sensible to me.

    UPDATE: From looking at the tutorial you linked to, I take it that there is a lot of suboptimal information in there. Take a look at the following piece of “Ruby”, for example:

    ranks = state_data.sort_by{|state, counter| counter}.collect{|state, counter| state}.reverse
    state_data = state_data.sort_by{|state, counter| state}
    
    state_data.each do |state, counter|
      puts "#{state}:\t#{counter}\t(#{ranks.index(state) + 1})"
    end
    

    You could write this much more cleanly and get the same result:

    rank = state_data.sort_by(&:last)
    state_data.sort.each do |data|
      puts "%s:\t%d\t(%d)" % [*data, rank.index(data) + 1]
    end
    

    The author also recommends code like

    filename = "output/thanks_#{lastname}_#{firstname}.html"
    output = File.new(filename, "w")
    output.write(custom_letter)
    

    While the Ruby idiom would be:

    filename = "output/thanks_#{lastname}_#{firstname}.html"
    File.open(filename, 'w') { |f| f.write(custom_letter) }
    

    This shows that the author doesn’t seem to be very apt in Ruby. Thus, I can’t recommend that tutorial.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to loop through a bunch of documents I have to put

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.