>> User.ancestors
=> [User(...), ActiveRecord::Base, ActiveRecord::Aggregations, ActiveRecord::Transactions,
ActiveRecord::Reflection, ActiveRecord::Serialization, ActiveModel::Serializers::Xml,
ActiveModel::Serializers::JSON, ActiveModel::Serialization, ActiveRecord::AutosaveAssociation,
ActiveRecord::NestedAttributes, ActiveModel::SecurePassword, ActiveRecord::IdentityMap,
ActiveRecord::Associations, ActiveRecord::NamedScope, ActiveModel::Validations::Callbacks,
ActiveRecord::Callbacks, ActiveModel::Observing, ActiveRecord::Timestamp,
ActiveModel::MassAssignmentSecurity, ActiveRecord::AttributeMethods::Dirty, ActiveModel::Dirty, ActiveRecord::AttributeMethods::TimeZoneConversion, ActiveRecord::AttributeMethods::PrimaryKey,
ActiveRecord::AttributeMethods::Read, ActiveRecord::AttributeMethods::Write,
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "sqlite3" | |
| SQLite3::Database.new(":memory:") do |db| | |
| rows = db.execute <<-SQL | |
| create table numbers ( | |
| name varchar(30), | |
| val int | |
| ); | |
| SQL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Composite | |
| def initialize(obj) | |
| @target = obj | |
| end | |
| def extend(component) | |
| @target = self.class.new(component.new(@target)) | |
| end | |
| def method_missing(id, *args, &block) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "forwardable" | |
| class BigAssObject | |
| def initialize | |
| @sub_object_1 = Something.new | |
| @sub_object_2 = SomethingElse.new | |
| @sub_object_3 = SomethingEEEELSE.new | |
| end | |
| delegate [:a, :b, :c, :d] => :sub_object_1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| eval(DATA.read) # :trollface: | |
| ## USE VELLUM TO DESCRIBE A DRAWING IN THE ABSTRACT | |
| drawing = Vellum::Drawing.new(300,400) | |
| drawing.layer(:box) do |g| | |
| g.rect(g.top_left, g.width, g.height) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ARTICLE_DATA = { 1 => { :status => "published" }, | |
| 2 => { :status => "draft" } } | |
| module BrokenRecord | |
| class Table | |
| def initialize(record_class) | |
| @record_class = record_class | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class A | |
| def a | |
| b | |
| end | |
| def b | |
| puts "YAY" | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class A | |
| def initialize | |
| @b = B.new | |
| end | |
| def method_missing(m, *a, &b) | |
| @b.public_send(m, *a, &b) # changing this to use send() fixes the error | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "minitest/autorun" | |
| require "delegate" | |
| require "sqlite3" | |
| describe "Mapping fields in results" do | |
| before do | |
| BrokenRecord.database = SQLite3::Database.new(":memory:") | |
| BrokenRecord.database.execute %{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def update(id, params) | |
| # FIXME: This is not secure | |
| sql = params.map { |k,v| "#{k} = #{v.inspect}" }.join(", ") | |
| # BrokenRecord.database is just an instance of Sqlite::Database | |
| BrokenRecord.database.execute %{ | |
| update #{@table_name} | |
| set #{sql} | |
| where id = #{id} | |
| } |