require 'net/http'
def correctIpAddress?(source)
begin
url = URI.parse(source)
rescue URI::InvalidURIError
return false
rescue StandardError
raise
end
host = url.host
begin
ip = IPAddr.new host
rescue IPAddr::InvalidAddressError
return false
rescue StandardError
raise
end
return true
end
puts correctIpAddress?('https://example.com/foo') # valid url, no ip address
puts correctIpAddress?('foo bar 42 $%$%') # invalid url
puts correctIpAddress?('https://127.0.0.1/foo') # valid url, valid ip address
> arr = {:name => "Tom", :surname => "Johns", :age => "100", :name => "Jack", :surname => "Hill", :age => "20"}
(irb):1: warning: key :name is duplicated and overwritten on line 1
(irb):1: warning: key :surname is duplicated and overwritten on line 1
(irb):1: warning: key :age is duplicated and overwritten on line 1
hash = {foo: 42, foo: 43} # => { foo: 42 }
# frozen_string_literal: true
# require dependence gems
require 'net/http' # for http request
require 'json' # for JSON parse
# create URI instance
url = URI.parse 'https://openexchangerates.org/api/latest.json?app_id=60da2bd9b3064714b2c5f2e8b00fbd40%22'
# create simple HTTP GET request
response = Net::HTTP.get_response url
# get body from response
response_body = response.body
# magic! parse response_body from JSON to Ruby Hash class.
#
# second argument is optional
# convert String keys to Symbol, ex. { "foo" => 42 } to { foo: 42 }
result = JSON.parse response_body, symbolize_names: true
# print rates for USD
puts result[:rates][:USD]
class String
# @see String#capitalize
def capitalize_words(separator = ' ')
words = split(separator).map(&:capitalize)
words.join separator
end
end
describe String do
describe '#capitalize_words' do
context 'with word' do
let :one_word do
'foo'
end
it 'return capitalized word' do
expect(one_word.capitalize_words).to eq 'Foo'
end
end
context 'with words' do
let :words do
'foo bar'
end
it 'return capitalized all words' do
expect(words.capitalize_words).to eq 'Foo Bar'
end
end
context 'with custom separator' do
let :words do
'foo$bar'
end
let :custom_separator do
'$'
end
it 'return capitalized with custom separator' do
expect(words.capitalize_words(custom_separator)).to eq 'Foo$Bar'
end
end
context 'with empty' do
let :empty do
''
end
it 'return empty' do
expect(empty.capitalize_words).to eq empty
end
end
end
end
innerText
искомого элемента <script>
.require 'nokogiri'
api_body_result = <<-HTML
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>500 Server Error</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Server Error</h1>
<h2>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. </h2>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. .</p>
</body>
</html>
HTML
doc = Nokogiri::HTML api_body_result
# source html inner text
text = doc.text
# or
# html title
title = doc.title
# html body
element = doc.at_css 'body'
body = element.text
# Technology
class Technology
attr_accessor :val
def initialize
@val = 42
yield self
end
end
# MyClass2
class MyClass
# @param [Integer] val
# @return [NilClass]
def a_method(val)
puts val
end
# @return [NilClass]
def b_method
Technology.new do |technology|
a_method technology.val
end
end
end
my_class = MyClass.new
my_class.b_method # => 42