Created
March 4, 2012 03:27
-
-
Save cookrn/1970443 to your computer and use it in GitHub Desktop.
dojo4 quiz #2
This file contains 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
// 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" |
This file contains 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
=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" |
This file contains 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
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 |
This file contains 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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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