Skip to content

Instantly share code, notes, and snippets.

View swanandp's full-sized avatar
🖥️

Swanand Pagnis swanandp

🖥️
View GitHub Profile
@swanandp
swanandp / posts_service.rb
Last active December 27, 2019 16:56
Interacting with a RESTful posts service using HTTParty
class PostsService
include HTTParty
base_uri "https://api.example.com"
read_timeout 5 # always have timeouts!
# debug_output $stdout # for quick access during debugging
attr_reader :auth, :headers
def initialize(user)
@swanandp
swanandp / And-dot-demo.rb
Created May 31, 2017 08:47
Ruby's &. operator is deceptive. It doesn't behave the same at foo && foo.something when foo is false.
irb(main):004:0> false&.foo
NoMethodError: undefined method `foo' for false:FalseClass
from (irb):4
from /Users/swanand/.rbenv/versions/2.3.1/bin/irb:11:in `<main>'
irb(main):005:0> false && false.foo
=> false
irb(main):006:0>
# Valid syntax
def do_stuff(arg1)
do_something(arg1)
rescue AnError => e
process_error(e)
end
# Syntax Error
do_stuff = lambda do |arg1|
do_something(arg1)

The Sum and Product puzzle

There are two natural numbers m, n greater than 1: their sum does not exceed 100. Now Sarah knows the sum s = m + n, and Paul knows the product p = m * n.

They have the following dialogue:

Paul: I don't know what m and n are.
Sarah: No, I knew that you didn't know. And I don't know what they are either.
Paul: Oh, but now I do know!

@swanandp
swanandp / bsearch.rb
Created November 16, 2014 09:10
Binary Search Implementation and benchmarks in Ruby
def iterative_bsearch(a, value)
low, hi = get_limits(a)
while low < hi
mid = (low + hi) / 2
if a[mid] == value
return mid
elsif a[mid] < value
low = mid + 1
else
hi = mid
@swanandp
swanandp / sequs_112.clj
Last active August 29, 2015 14:07
4Clojure 112 Sequs Horribilis
(ns fourclojure.sequs-112)
;; https://www.4clojure.com/problem/112
(declare sequs reducer)
(defn flatsum [v] (apply + (flatten v)))
(defn present? [_ el] (seq el))
javax.servlet.jsp.JspException: No bean found for attribute key WelcomeNote
at org.apache.struts.taglib.template.InsertTag.doEndTag(InsertTag.java:133)
at booking._0002fbooking_0002fplanner_0002ejspplanner_jsp_0._jspService(_0002fbooking_0002fplanner_0002ejspplanner_jsp_0.java:937)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:126)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.runtime.JspServlet$JspServletWrapper.service(JspServlet.java:174)
at org.apache.jasper.runtime.JspServlet.serviceJspFile(JspServlet.java:274)
at org.apache.jasper.runtime.JspServlet.service(JspServlet.java:387)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.broadvision.servlet.ServletContainer.service(ServletContainer.java:415)
@swanandp
swanandp / console.rake
Created April 28, 2014 07:38
Rake task to load up your gem in a pry repl
desc "Open an irb (or pry) session preloaded with this gem"
task :console do
begin
require 'pry'
gem_name = File.basename(Dir.pwd)
sh %{pry -I lib -r #{gem_name}.rb}
rescue LoadError => _
sh %{irb -rubygems -I lib -r #{gem_name}.rb}
end
end
module HookMethodMissing
def method_missing(name, *args, &block)
puts name
super
end
end
class A
extend HookMethodMissing
end
2012-03-29T10:25:10+00:00 app[app.1]: #bangalore-ruby <keyword> tweet - Returns a link to a tweet about <keyword>
2012-03-29T10:25:10+00:00 app[app.1]: <travis me> sferik/rails_admin - Returns the build status of https://github.com/sferik/rails_admin
2012-03-29T10:25:10+00:00 app[app.1]: <user> is a badass guitarist - assign a role to a user
2012-03-29T10:25:10+00:00 app[app.1]: <user> is not a badass guitarist - remove a role from a user
2012-03-29T10:25:10+00:00 app[app.1]: Find the build status of an open-source project on Travis
2012-03-29T10:25:10+00:00 app[app.1]: animate me <query> - The same thing as `image me`, except adds a few
2012-03-29T10:25:10+00:00 app[app.1]: convert me <expression> to <units> - Convert expression to given units.
2012-03-29T10:25:10+00:00 app[app.1]: help <query> - Displays all help commands that match <query>.
2012-03-29T10:25:1
0+00:00 app[app.1]: help - Displays all of the help commands that bangalorehubot knows about.