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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:34:43+00:00 2026-06-14T13:34:43+00:00

I want to do two things: Convert IP Address inputs into CIDR Here are

  • 0

I want to do two things: Convert IP Address inputs into CIDR
Here are some example inputs:

1.1.1.1    
192.168.*.* #=> 192.168.0-255.0-255
192.168.1.2-20
1.1.1-10.1-100

Check if a given IP Address falls into any CIDR. This must be a very fast query, as it’s a very common lookup in my web app. I’m thinking of doing something like this:

def matches?(request)
  valid = @ips.select {|cidr| cidr.contains?(request.remote_ip) }
  !valid.empty?
end

I think converting IP ranges into CIDR will let lookups be faster than what we’re doing now, which is breaking the IP’s into integer octets. We then index the first two sets of octets to partially match against IP’s. Another option might be converting everything to ints and doing comparisons that way. I’d convert to ints with something like this IPAddr.new("1.1.1.1").to_i but then I’d need to store an upper and lower IP for each range instead of just a single CIDR.

Please let me know if I am overlooking any mainstream approaches, popular gems or repo’s. Thanks!

  • 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-14T13:34:45+00:00Added an answer on June 14, 2026 at 1:34 pm

    Well, to get the CIDR notation of a range, you need an IP and the number of network bits (calculated from the netmask).

    To enumerate the addresses of a given range, you can use the NetAddr (< 2.x) gem.

    p NetAddr::CIDR.create('192.168.1.0/24').enumerate
      => ['192.168.1.0', '192.168.1.1', '192.168.1.2'... '192.168.1.255']
    

    You can also calculate the bits from the netmask on the fly:

    mask_int = NetAddr.netmask_to_i('255.255.255.0')
    p NetAddr.mask_to_bits(mask_int)
      => 24
    

    And to create a range based on two IPs:

    lower = NetAddr::CIDR.create('192.168.1.1')
    upper = NetAddr::CIDR.create('192.168.1.10')
    p NetAddr.range(lower, upper)
      => ['192.168.1.2', '192.168.1.3'... '192.168.1.9']
    

    So now that you can create a CIDR range, you can check to see if an IP is a part of it:

    cidr = NetAddr::CIDR.create('192.168.1.0/24')
    p cidr.contains?('192.168.1.10')
      => true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to do two things in my browser: Load a text file into
First of all I want to mention two things, One: My code isn't perfect
I want to apply a different hyper link style to the following two things:
For some complicated reason, I want to convert any supported type T (coming from
I'm in need of some help :s I want two animations to launch when
I am still trying to convert mysql_* things into PDO and prepared statements. It
I want these two print functions to do the same thing: unsigned int Arraye[]
I found strange generics behaviour. In two words - thing I realy want is
I want two special methods: one that runs for all URLs one that runs
Suppose we want two constructors for a class representing complex numbers: Complex (double re,

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.