Skip to content

Instantly share code, notes, and snippets.

@carsontang
Created December 13, 2012 01:15
Show Gist options
  • Save carsontang/4273203 to your computer and use it in GitHub Desktop.
Save carsontang/4273203 to your computer and use it in GitHub Desktop.
Ruby metaprogramming examples
module RubyLang
end
class Computer
def self.my_attr_reader(name)
if name.is_a? Symbol
puts "is symbol"
else
puts "is string"
end
define_method(name) {
self.instance_variable_set(name, nil)
self.instance_variable_get(name)
}
end
my_attr_reader :blah
my_attr_reader :port
end
# module Dakar
# class RtspClient
# client_methods = %w(describe setup options play teardown pause get_parameter set_parameter)
# methods = []
# client_methods.each { |method| methods << method.to_sym }
# methods.each do |method_name|
# define_method method_name.to_sym do |uri|
# puts "Called #{method_name} with #{uri}"
# end
# end
# def set_parameter(uri)
# puts "Calling SET_PARAMETER"
# end
# def initialize(host)
# def component(name)
# info = @data_source.send("get_#{name}_info", @id)
# price = @data_source.send("get_#{name}_price", @id)
# end
# end
# end
# class A
# private
# def method1
# puts "inherited"
# end
# end
# class B < A
# def method2
# puts "in method 2"
# method1
# end
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment