Someone help to comment this RUBY code to understand, please require \'socket\'
ID: 3810640 • Letter: S
Question
Someone help to comment this RUBY code to understand, please
require 'socket'
def open_port(host, port)
sock = Socket.new(:INET, :STREAM)
raw = Socket.pack_sockaddr_in port, host
puts "#{port} open." if sock.connect(raw)
rescue(Errno::ECONNREFUSED)
rescue(Errno::ETIMEDOUT)
end
def main(host, start_port, end_port)
until start_port==end_port do
open_port(host, start_port)
start_port +=1
end
end
main ARGV[0], ARGV[1].to_i, ARGV[2].to_i
require 'rubygems'
require 'nokogiri'
require 'open-uri'
page=Nokogiri::HTML(open("https://en.wikipedia.org/wiki/Computer_security"))
parse = page.xpath("//text()").text
re = /(S+s+){0,4}[sS]ecurity(s+S+){0,4}/
# Either security or Security between 0-4 words separated with spaces
count =0;
parse.gsub(re) do |item|
count +=1
puts " Match #{count} #{item.to_s} "
end
# globally substitute re. For each item print current item
#.to_s converts to string
require 'json'
require 'pp'
require 'net/http'
url = 'http://api.apixu.com/v1/current.json?key=72758ce44d72440a818195031171902&q=Denver,CO'
uri = URI(url)
response = Net::HTTP.get(uri)
j=JSON.parse(response)
pp j
puts "The temperature in #{j['location']['name']} is #{j['current']['temp_f']} degrees."
Explanation / Answer
require 'socket' #This will include the sockets standard library file.
#sockets are endpoints communication channel, to communicate between same machine or different machine.
def open_port(host, port) #This is like a method/function with host and port argument. In ruby the method should be started with lowercase. Otherwise(Uppercase) it will consider as a constant.
sock = Socket.new(:INET, :STREAM) #create new sock object -- (:INET, :STREAM) -- TCP socket. INET - comminucation domain, STREAM - socket type
raw = Socket.pack_sockaddr_in(port, host) #Packs port and host as an AF_INET/AF_INET6 sockaddr string and store the result to 'raw' variable.
puts "#{port} open." if sock.connect(raw) #it print the command with result of socket connection. sock.connect - connect socket.
rescue(Errno::ECONNREFUSED) #if any failure occure during socket connection, then it will throw the exception with this message. The target 'raw' was not listening for connections refused the connection request
rescue(Errno::ETIMEDOUT) # Throw timeout error
end #enf function/method of open_port
def main(host, start_port, end_port) #main function/method with 3 argument of host, start_port and end_port.
until start_port==end_port do #it's one type of looping method in ruby. This will continue to process untill start_port is equal to end_port.
open_port(host, start_port) #call open_port method/function to connect socket, and try to connect socket with several port info.
start_port +=1 #increase start_port value with one for each and every looping.
end #end until loop
end #end main function/method
main ARGV[0], ARGV[1].to_i, ARGV[2].to_i #fucntion call. call main fucntion with 3 arguments. ARGV[0] - host, ARGV[1].to_i - start_port, ARGV[2].to_i - end_port.
#to_i - it will consider only integer.
require 'rubygems'
require 'nokogiri'
require 'open-uri'
page=Nokogiri::HTML(open("https://en.wikipedia.org/wiki/Computer_security")) #open or load html page
parse = page.xpath("//text()").text #It will parse the text of the xpath element.
re = /(S+s+){0,4}[sS]ecurity(s+S+){0,4}/ # Either security or Security between 0-4 words separated with spaces
count =0; #initialize count value with 0
parse.gsub(re) do |item| #gsub is one type of regular expression in ruby. It will sreach match and return the pattern based on 're' expression.
count +=1 #increment the count
puts " Match #{count} #{item.to_s} " # print the match count value.
end #end do loop
# globally substitute re. For each item print current item
#.to_s converts to string
require 'json'
require 'pp'
require 'net/http' #ruby standard library
url = 'http://api.apixu.com/v1/current.json?key=72758ce44d72440a818195031171902&q=Denver,CO' # assign to url
uri = URI(url) #load url, URI module providing classes to handle the Uniform Resource Identifiers
response = Net::HTTP.get(uri) #get HTTP header request and store response
j=JSON.parse(response) #parse respose with json object
pp j #Pretty-printer for Ruby objects
puts "The temperature in #{j['location']['name']} is #{j['current']['temp_f']} degrees." #display the result with parsed JSON objects
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.