Skip to content

Instantly share code, notes, and snippets.

class Person
attr_accessor :name
def method_missing(method_name, *args, &block)
if method_name == :email
"[email protected]"
else
"Hey, you just called the #{method_name} method /
With these arguments: #{args.join(' ')} /
But there is no such method"
class Tree
# Pretend this is a real implementation
def find_node(conditions = {})
"find the node by #{conditions.inspect}"
end
def method_missing(method_name, *arguments, &block)
return super unless method_name.to_s =~ /^find_node_by_(.*)$/
find_node($1.to_sym => arguments.first)
end
class Person
def method_missing(m, *args, &block)
puts "Method Missing: Called #{m} with #{args.inspect} and #{block}"
end
def hello
puts "Hello from class Person"
end
end
class Student
def law_fees
21000
end
def physical_science_fees
22000
end
class Student
# ommitted
def fees(school)
send("#{school}_fees")
end
def method_missing(method_name)
return super unless /^.+_fees$/ =~ method_name
20000
end
Rails.application.routes.draw do
root 'home#index'
resources :events, only: [:index, :show]
end
namespace :fake do
desc "Generate Fake Data
task :data => :environment do
generate_fake
end
end
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :first_name
t.string :last_name
t.string :email
t.timestamps null: false
end
end
end
class Person < ActiveRecord::Base
validates :email_confirmation, presence: true
validates :name, length: { minimum: 2 }
validates :bio, length: { maximum: 1000,
too_long: "%{count} characters is the maximum allowed" }
end
describe Array do
describe "includes_subset?" do
it "finds subsets" do
a = [1,2,3,4,5]
b = [1,2]
expect(a.includes_subset?(b)).to eq(true)
end
end
end