Skip to content

Instantly share code, notes, and snippets.

@cookrn
Created March 4, 2012 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cookrn/1970443 to your computer and use it in GitHub Desktop.
Save cookrn/1970443 to your computer and use it in GitHub Desktop.
dojo4 quiz #2
// QUESTION
// write a javascript class, called 'Foo', with a class method 'bar' and an
// instance method 'bar'
var Foo = function Foo(){
console.log('new Foo instance');
};
Foo.bar = function(){
console.log('Foo::bar');
}
Foo.prototype.bar = function(){
console.log('Foo#bar');
}
Foo.bar() // => "Foo::bar"
var baz = new Foo;
baz.bar() // => "Foo#bar"
=begin
QUESTION
write a ruby class, called 'Foo', and a mixin, 'Bar', that adds a
*class_method* 'baz' and an *instance_method* 'baz'
=end
module Bar
def self.included( klass )
klass.extend ClassMethods
klass.include InstanceMethods
end
module ClassMethods
def baz
p "class baz"
end
end
module InstanceMethods
def baz
p "instance baz"
end
end
end
class Foo
include Bar
end
Foo.baz # => "class baz"
Foo.new.baz # => "instance baz"
QUESTION
what's wrong with this code?
=> buffer.match( /^foo.*bar/ )
Answer :: The regex will greedy match on the '.*' and never hit the 'bar' at the end
# QUESTION
# what's wrong with this code?
class Farm
include Mongoid::Document
before_save do |farm|
farm.ensure_no_blank_comments!
end
def ensure_no_blank_comments!
errors.add('teh comments are fubar') if
comments.any?{|comment| comment.content.blank?}
end
class Comment
embedded_in :farm
field :content, String
end
embeds_many :comments
end
farm = Farm.find(id)
params = { :content => nil }
comment = farm.comments.build(params)
comment.save!
=begin
* Mongoid does not automatically respect nested classes for embedded documents
* The Comment class is not including Mongoid::Document
* The before save check for no blank comments is on the Farm and is not
automatically triggered when saving an embedded object. Instead, after adding a
comment with empty content, the Farm object itself will fail to save
=end
@ahoward
Copy link

ahoward commented Mar 5, 2012

alll great save #3. it does match, because it's greedy, but it will backtrack. this gives exponentially bad performance on strings like 'foo (long buffer ) b (long buffer ) ba (long buffer ) ba (long buffer ) b (long buffer) bar'. best summary link i could find before 1st cofee: http://www.codinghorror.com/blog/2006/01/regex-performance.html

@cookrn
Copy link
Author

cookrn commented Mar 5, 2012

Thanks for the link!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment