7a1f5bc20a17bba473d54617d9893466

I'm new to ruby and I make this quick program to check if a domain is available, can we improve it?

1
2
3
4
5
6
7
8
9
10
11
12
13
class WhoIsQuery
    query = ARGV[0]
    puts "Querying whois with #{query} ..."
    results = IO.popen("whois -Q #{query}")
    message = "No more domain for you!!"
    results.readlines.each do |line|
        if line.include?("No match for ")
            message = "Domain waiting for you..."
            break
        end
    end
    puts message
end

Refactorings

No refactoring yet !

5170ca260dbd2cdfd5a887a4dba7636f

Jeremy Weiskotten

May 14, 2008, May 14, 2008 19:31, permalink

No rating. Login to rate!

You can use Enumerable#any? instead of #each with a break...

1
2
3
4
5
6
7
8
9
10
11
12
13
class WhoIsQuery
    query = ARGV[0]
    puts "Querying whois with #{query} ..."
    results = IO.popen("whois -Q #{query}")
    
    message = if results.readlines.any? { |line| line.include?("No match for") }
      "Domain waiting for you..."
    else
      "No more domain for you!!"
    end
    
    puts message
end
45f288d7e7bd53adb53864ab3ffb32ef

Andrea Ortiz

May 17, 2008, May 17, 2008 17:17, permalink

No rating. Login to rate!

YES

Af4ce9309f8c4f7fc5cb33e7a5b08c64

coderrr

May 23, 2008, May 23, 2008 17:34, permalink

No rating. Login to rate!

no point in having the class if you aren't using it for anything

1
2
3
4
5
6
7
8
9
query = ARGV[0]
puts "Querying whois with #{query} ..."

message = if `whois -Q #{query}` =~ /no match for/i
  then "Domain waiting for you..."
  else "No more domain for you!!"
end

puts message
Avatar

hugh Bien

May 23, 2008, May 23, 2008 17:55, permalink

No rating. Login to rate!

make it one line for fun =]

1
puts "Querying whois with #{ARGV[0]} ...\n" + (`whois -Q #{ARGV[0]}` =~ /no match for/i ? "Domain waiting for you..." : "No more domain for you!!")
E7be134a6de3d308adf765be1004c450

Peter Harkins

May 23, 2008, May 23, 2008 18:19, permalink

No rating. Login to rate!

There are three Ruby whois libraries, you don't have to reinvent the wheel. And whois doesn't necessarily tell you that a domain is available for registration, you should be doing a DNS lookup.

Your refactoring





Format Copy from initial code

or Cancel