Skip to content

Instantly share code, notes, and snippets.

@smtalim
smtalim / dsl1.rb
Created September 27, 2011 11:37
Collection of programs
class Rubyist
def welcome(*args)
"Welcome " + args.join(' ')
end
end
obj = Rubyist.new
puts(obj.send(:welcome, "famous", "Rubyists")) # => Welcome famous Rubyists
@smtalim
smtalim / chess_opener.rb
Created September 28, 2011 03:41
Collection of programs
class ChessOpener
def initialize
@data = {}
load_data
end
def self.load(filename)
dsl = new
dsl.instance_eval(File.read(filename))
end
@smtalim
smtalim / anonymous_class.rb
Created August 7, 2012 09:55
Anonymous Class
# Here are some ways by which you can define an anonymous class
# 1
class Rubyist
def self.who
"Geek"
end
end
# 2
Free online courses at RubyLearning:
a. Ruby Metaprogramming 8th batch -
http://rubylearning.com/blog/2013/02/08/a-free-ruby-metaprogramming-course-learn-to-think-in-ruby/
b. Sinatra 101 10th batch -
http://rubylearning.com/blog/2013/01/25/a-free-online-course-sinatra-101/
c. Programming the Web with Ruby 3rd batch -
http://rubylearning.org/classes/
@smtalim
smtalim / SinatraExt
Created July 12, 2013 04:07
Let us say that our app requires us to send both GET and POST requests to a particular URL such that the same block of code handles both verbs. It makes sense to define an extension that can handle our requirement, as we don’t want to define two routes with identical code.
** Creating Sinatra Extensions **
Let us say that our app requires us to send both GET and POST requests to a particular URL such that the same block of code handles both verbs. It makes sense to define an extension that can handle our requirement, as we don’t want to define two routes with identical code.
Here is our code:
# post_get.rb
require 'sinatra/base'
module Sinatra
module PostGet
class MotorCycle
def initialize(make, color)
# Instance variables
@make = make
@color = color
end
def startEngine
if (@engineState)
'Engine is already Running'
else
class Display < MotorCycle
def dispAttr
'Color of MotorCycle is ' + @color
'Make of MotorCycle is ' + @make
end
end
class Hiya
def self.hiya
"Hiya all!"
end
end
require 'ruboto/widget'
require 'ruboto/util/toast'
ruboto_import_widgets :Button, :LinearLayout, :TextView
# http://xkcd.com/378/
class QuickStartActivity
def onCreate(bundle)
super
# myapp3_1.rb
require 'sinatra'
set :display_string, 'Welcome from RubyLearning again!'
before do
@msg = 'Awesome!'
end
get '/' do
settings.display_string + " " + @msg