Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
# Hello World Program in Ruby
require 'uri'
require 'net/http'
require 'json'
require 'ostruct'
module Heroku
module Addons
class Addon
attr_accessor :name
def initialize(attrs)
@name = attrs['name']
end
end
class AddonPlan
attr_accessor :description, :price, :unit
def initialize(attrs)
@description = attrs['description']
@price = attrs['price']['cents'] / 100.0
@unit = attrs['price']['unit']
end
def full_price
"$#{'%.2f' % price}/#{unit}"
end
end
class Client
def initialize
uri = URI.parse("https://api.heroku.com/")
@http = Net::HTTP.new(uri.host, uri.port)
@http.use_ssl = true if uri.scheme == 'https'
@headers = {
'Accept' => 'application/vnd.heroku+json; version=3'
}
end
def request(path, headers={})
response = @http.get(path, @headers.merge(headers))
JSON.parse response.body
end
end
@client = Client.new
# Loads all the available addons from Heroku
def self.all
@client.request('/addon-services').map { |addon| Addon.new(addon) }
end
def self.plans(addon)
@client.request("/addon-services/#{addon}/plans").map { |plan| AddonPlan.new(plan) }
end
end
end
#########################################################################################
addons = Heroku::Addons.all
addons.each do |addon|
name = addon.name
plans = Heroku::Addons.plans(name)
puts name
plans.sort_by(&:price).each do |plan|
puts " #{plan.description}: #{plan.full_price}"
end
end
#until loop
counter = 1
until counter == 10
puts counter
counter +=1
end
puts counter
#until loop
i = 0
until i == 6
i+=1
end
puts i
#while loop
counter = 1
while counter <11
puts counter
counter +=1
end
#case example
user_input = $stdin.gets.chomp
user_input.downcase!
case user_input
when 3
puts "£££"
when 2
puts "££"
when 1
puts "£"
end
#Case Example.
current_energy = 3
case current_energy
when 3
puts "Energy is 3"
when 2
puts "Energy is 2"
when 1
puts "Energy is 1"
else
puts "Nil"
end
# @param {String} s
# @param {String} t
# @return {Boolean}
def is_anagram(s, t)
hash = Hash.new(0)
hash1 = Hash.new(0)
s.each_char do |v|
hash.store(v, hash[v]+1)
end
t.each_char do |v|
hash1.store(v, hash1[v]+1)
end
p hash == hash1
end
is_anagram("rat","cat")
def majority_element(nums)
p [] if nums.empty?
hash =Hash.new(0)
nums.each do |v|
hash.store(v, hash[v]+1)
end
tmp = []
hash.each do |key, value|
p value
if value > nums.length/3
tmp << key
end
end
p tmp
end
majority_element([1,2])