z, ? | toggle help (this) |
space, → | next slide |
shift-space, ← | previous slide |
d | toggle debug mode |
## <ret> | go to slide # |
c, t | table of contents (vi) |
f | toggle footer |
g | toggle follow |
r | reload slides |
n | toggle notes |
p | run preshow |
P | toggle pause |
s | choose style |
class Car
attr_accessor :color
def initialize(color)
@color = color
end
def car_method
end
end
class Mazda < Car
attr_accessor :how_many_wheels
def initialize(color, how_many_wheels)
@how_many_wheels = how_many_wheels
super(color)
end
def mazda_method
end
end
s = "Very cool string"
puts s.say_hello # => Welcome!
class String
def say_hello
"Welcome!"
end
end
s = "Very cool string"
puts s.say_hello # => Welcome!
mazda = Mazda.new("red", 4)
puts mazda.instance_variables # => @color, @how_many_wheels
puts mazda.class.instance_methods(false) # => mazda_method
class Car
attr_accessor :color
def initialize(color)
@color = color
end
def car_method
end
end
Car = Class.new do
attr_accessor :color
def initialize(color)
@color = color
end
def car_method
end
end
mazda = Mazda.new("red", 4)
puts mazda.class # => Mazda
mazda = Mazda.new("red", 4)
puts mazda.class # => Mazda
puts Mazda.superclass # => Car
mazda = Mazda.new("red", 4)
puts mazda.class # => Mazda
puts Mazda.superclass # => Car
puts Car.superclass # => Object
mazda = Mazda.new("red", 4)
puts mazda.class # => Mazda
puts Mazda.superclass # => Car
puts Car.superclass # => Object
puts Object.superclass # => BasicObject
mazda = Mazda.new("red", 4)
puts mazda.class # => Mazda
puts Mazda.superclass # => Car
puts Car.superclass # => Object
puts Object.superclass # => BasicObject
puts BasicObject.superclass # => nil
puts Mazda.ancestors # => [Mazda, Car, Object, Kernel, BasicObject]
puts Mazda.ancestors # => [Mazda, Car, Object, Kernel, BasicObject]
require 'rubygems'
gem 'rails'
module Kernel
def gem(gem_name, *requirements)
...
end
end
require "rubygems"
class Gyminator
def load_rails
gem "rails"
end
end
g = Gyminator.new
g.load_rails
mazda.mazda_method()
puts "Test"
puts self # => main
# test.rb:35:in `test': wrong number of arguments (0 for 2..3) (ArgumentError)
# from test.rb:35:in `<main>'
class Mazda < Car
def mazda_method
another_method
end
def another_method
puts "Something in another method"
end
end
m = Mazda.new("red", 4)
m.mazda_method # => "Something in another method"
class Mazda < Car
def mazda_method
puts self # => #<Mazda:0x007fe06190f0f8>
another_method
end
def another_method
puts "Something in another method"
end
end
m = Mazda.new("red", 4)
m.mazda_method # => "Something in another method"
class Klass
def initialize
@secret = 99
end
end
k = Klass.new
k.instance_eval { @secret } #=> 99
class Klass
def initialize
@secret = 99
end
end
k = Klass.new
k.instance_eval { @secret } # => 99
k.instance_eval { puts self } # => #<Klass:0x007fa8da8113b8>
class Klass
def initialize
@secret = 99
end
end
k = Klass.new
k.instance_eval { @secret } #=> 99
k.instance_eval { puts self } # => #<Klass:0x007fa8da8113b8>
Klass.class_eval { puts self } # => Klass
Class EssApi
attr_accessor :api
def initialize
authenticate
end
def authenticate
....
end
def get_employer(id)
@api.get_employer(id)
end
def get_jobseeker(id)
@api.get_jobseeker
end
end
Class EssApi
def get_epp(id)
@api.get_epp(id)
end
def get_jsci(id)
@api.get_jsci(id)
end
end
mazda.mazda_method()
mazda.mazda_method()
mazda.send(:mazda_method)
Class EssApi
@api.available_methods # => [:get_client, :get_employer, :get_epp, :get_jsci]
@api.available_methods.each do |method|
define_method(method) do |id|
@api.send method, id
end
end
end
Class EssApi
def method_missing(method, *args)
@api.send method, args[o]
end
end
Class EssApi
def method_missing(method, *args)
unless ["get_client", "get_employer", "get_epp", "get_jsci"].include? method
super
else
@api.send method
end
end
end
Class EssApi
def respond_to?(method)
["get_client", "get_employer", "get_epp"].include?(method) ? true : super
end
end
require 'ostruct'
test = OpenStruct.new
test.color = "red"
test.size = 14
test.color # => "red"
class MyOpenStruct
def initialize
@attributes = {}
end
def method_missing(name, *args)
attribute = name.to_s
if attribute =~ /=$/
@attributes[attribute.chop] = args[0]
else
@attributes[attribute]
end
end
end
client = Client.find(86)
puts client.name
puts client.address
def method_missing(method, *args, &block)
if respond_to_without_attributes?(method, true)
super
else
match = match_attribute_method?(method.to_s)
match ? attribute_missing(match, *args, &block) : super
end
end
car1 = Mazda.new("red", 4)
car2 = Mazda.new("blue", 4)
def car1.method_only_for_one_instance
puts "super!"
end
car1.method_only_for_one_instance # => "super!"
car2.method_only_for_one_instance # => undefined method
# => `method_only_for_one_instance'
class << self
end
class Car
def Car.class_method1
end
def self.class_method2
end
end
class Car
class << self
def class_method1
end
def class_method2
end
end
end
class Car
def some_class_method
puts "instance method"
end
class << self
def some_class_method
puts "class method"
end
end
end
class Mazda < Car
end
Mazda.some_class_method # => class method
module ActionDispatch
module Http
module URL
class << self
def extract_domain(host, tld_length = @@tld_length)
...
end
def extract_subdomains(host, tld_length = @@tld_length)
...
end
def extract_subdomain(host, tld_length = @@tld_length)
...
end
module ActiveRecord
class << self
attr_accessor :delegate
attr_accessor :disable_ddl_transaction
end
def self.check_pending!
raise ActiveRecord::PendingMigrationError if Migrator.needs_migration?
end
MetaProgramming - Extending Ruby for Fun and Profit http://www.infoq.com/presentations/metaprogramming-ruby