Skip to content

Instantly share code, notes, and snippets.

View idimitrov07's full-sized avatar
🏠
Working from home

Ivan Dimitrov idimitrov07

🏠
Working from home
View GitHub Profile
class Item
def initialize(item_name)
@item_name = item_name
end
# def show
# puts "Instance method show for '#{self}'"
# end
# def to_s
class Animal
def move
"I can move"
end
end
class Bird < Animal
def move
super + " by flying"
end
class Rectangle
def initialize(length, breadth)
@length = length
@breadth = breadth
end
def perimeter
2 * (@length + @breadth)
end
end
require "benchmark"
def calculation_with_explicit_block(a, b, operation)
operation.call(a, b)
end
def calculation_with_implicit_block(a, b)
yield(a, b)
end
def number_shuffle(number)
no_of_combinations = number.to_s.size == 3 ? 6 : 24
digits = number.to_s.split(//)
combinations = []
combinations << digits.shuffle.join.to_i while combinations.uniq.size!=no_of_combinations
combinations.uniq.sort
end
# Given: 123
# Return: [123, 132, 213, 231, 312, 321]
class MyArray
attr_reader :array
def initialize(array)
@array = array
end
def sum(initial_value = 0)
return array.inject(0) {|sum, el| sum + el } + initial_value unless block_given?
sum = initial_value
class Restaurant
def initialize(menu)
@menu = menu
end
def cost(*orders)
# your code here
orders.inject(0) do |total_cost, order|
total_cost + order.keys.inject(0) {|cost, key| cost + @menu[key]*order[key] }
end
module Perimeter
# Your code here
def perimeter
sides.inject(0) {|sum, side| sum + side}
end
end
class Rectangle
# Your code here
include Perimeter
def calculate(*numbers)
#p numbers
if numbers[-1].is_a?(Hash)
return subtract(*(numbers[0...-1])) if numbers[-1][:subtract]
return add(*(numbers[0...-1])) if numbers[-1][:add]
else
add(*numbers)
end
end
function replaceWord(word){
var asterics = "";
for(var i = 0; i < word.length; i++){
asterics += "*";
}
return asterics;
}
function maskOutWords(words, text){
var maskedWord;