Why does this command not work in this particular code?

How can i do this? I tried

Net::HTTP.new(@site).head('/').kind_of? Net::HTTPOK 

but I get the following:

error: in `kind_of? ': class or module required (TypeError)

I think something is wrong with my code, can you take a look at it? I know that he is very dirty and rather bad, I am the first student in programming and I am studying. If you have any ideas for improvement let me know!

In addition, there is something corrupted by the ends contained inside. For some reason, I get an unexpected end error when I try to finish all defs.

Here is my code in context:

 def begindownload require 'net/http' puts "Enter the URL of the site that you want to rip images from (use www.*website*.com/folder/file.html or other extension format):" while @site = gets.chomp puts "Querying " + @site if Net::HTTP.new(@site).head('/').kind_of? Net::HTTPOK == true puts "Site is online!" else puts "Site is offline. Try again." end end end 
+4
source share
2 answers

In your code you use the line:

 if Net::HTTP.new(@site).head('/').kind_of? Net::HTTPOK == true 

Evaluated == , first calling Net::HTTPOK == true to evaluate to false . The rest of the statement will look like this:

 if Net::HTTP.new(@site).head('/').kind_of? false 

Call TypeError .

You do not need to check == true when you execute the if . If the statement after if evaluates to true , then if will be evaluated. Check if boolean == true is considered a bad form.

+6
source

Instead, you tried to use .instance_of?

0
source

Source: https://habr.com/ru/post/1332548/


All Articles