Skip to content

Instantly share code, notes, and snippets.

View aashish's full-sized avatar

Aashish Kiran aashish

  • India
View GitHub Profile
@aashish
aashish / date_validation.rb
Last active October 25, 2018 12:52
Date validation
date = ARGV[0]
class Date
def self.valid?(date)
y, m, d = date.split '-'
date.match(/^\d{4}-\d{2}-\d{2}$/) && Date.valid_date?(y.to_i, m.to_i, d.to_i)
end
end
@aashish
aashish / flattener.rb
Created May 13, 2019 15:24
Array of arrays will be flattened.
class Array
def flattener
arr = []
self.each do |x|
if x.is_a? Array
arr = arr + x.flattener
else
arr << x
end
@aashish
aashish / mars_rover_problem_with_proxy_pattern.rb
Last active August 18, 2020 15:50
Mars Rover Problem with proxy pattern
class Plateau
attr_accessor :max_x, :max_y, :direction
def initialize(x, y, direction = :n)
@max_x = x.to_i
@max_y = y.to_i
@direction = direction.downcase.to_sym
end
end
@aashish
aashish / mars_rover_problem_with_flyweight_pattern.rb
Created August 20, 2020 13:23
Mars Rover Problem with Flyweight pattern
class Plateau
attr_accessor :max_x, :max_y, :direction
def initialize(x, y, direction = :n)
@max_x = x.to_i
@max_y = y.to_i
@direction = direction.downcase.to_sym
end
def land(position)
@aashish
aashish / insert_image_on_pdf.rb
Last active February 21, 2024 17:18
Insert image on a existing PDF having content with hexapdf gem
require 'hexapdf'
doc = HexaPDF::Document.open("/home/xxxx/Downloads/OoPdfFormExample.pdf")
page = doc.pages[0]
canvas = page.canvas(type: :overlay)
canvas.translate(0, 20) do
canvas.fill_color(0.3, 0.7, 0.7)
canvas.rectangle(50, 0, 80, 80, radius: 80)