Let us say you have the following models:
class User
embeds_many :book_marks, :validate => false, :cascade_callbacks => true
end
class BookMark
field :url, type: string
field :category, type: string # personal, work
field :tags, type: Array
embedded_in :user
end
Let us say we wount to count the number of users with book marks in personal category with technology tags.
User.where(:"book_marks.category" => "personal",
:"book_marks.tags".in => ["technology"]).count
For the data set below, the query will return both rows.
name book_marks
category tags
============ ======== ====
Tom personal politics
work technology
Jack personal finance
personal technology
We have to modify the query so that we multiple attributes of a single embedded document. MongoDB provides a construct
called elem_match for such requirements.
User.where(:book_marks.elem_match =>
{ :category => "personal", :tags.in => ["technology"]).count
Now the query will only match Jack.
Using an array to initialize multiple variables
>> a,b=[1,2]
=> [1, 2]
>> a
=> 1
>> b
=> 2
When the array is longer than the LHS variable count
>> a,b,c=[1,2,3,4]
=> [1, 2, 3, 4]
>> a
=> 1
>> b
=> 2
>> c
=> 3
When the array is shorter than the LHS variable count
>> a,b,c=[1,2]
=> [1, 2]
>> a
=> 1
>> b
=> 2
>> c # initialized to nil
=> nil
Using splat(*) operator at the LHS of the assignment
>> a,b,*c=[1,2,3,4]
=> [1, 2, 3, 4]
>> a
=> 1
>> b
=> 2
>> c # array containing rest of the elements
=> [3, 4]
Did you know ?
>> a = 1,2 # is same as a = [1,2]
=> [1, 2]
>> a,b=1,2 # same as a,b = [1,2] OR a=1;b=2
=> [1, 2]
>> a
=> 1
>> b
=> 2
>> a = 1,b=2 # b is assigned to 2 but what about a?
=> [1, 2]
>> b
=> 2
>> a
=> [1, 2]
Add a file called webrick.rb file to the config\initializers directory.
if defined?(WEBrick::HTTPRequest)
WEBrick::HTTPRequest.const_set("MAX_URI_LENGTH", 10240)
end
Given array:
[1, 1, 1, 2, 3, 4, 4, 4, 4, 5, 9, 9, 9]
Desired array:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
To get the desired array we need to
Declare the array
a = [1, 1, 1, 2, 3, 4, 4, 4, 4, 5, 9, 9, 9]Remove the two
1’s at the beginning of the array.a[0..1] = [] # => [1, 2, 3, 4, 4, 4, 4, 5, 9, 9, 9]Remove three
4’s in the middle of the array.a[3..5] = [] # => [1, 2, 3, 4, 5, 9, 9, 9]Insert
6, 7, 8to the middle of the array.a[5..0] = [6,7,8] # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9]Remove the two
9’s at the end of the array.a[-2..-1] = [] # => [1, 2, 3, 4, 5, 6, 7, 8, 9]
Interesting tricks:
Inserting using
0terminated rangea[5..0] = [6,7,8]When the range is terminated by a
0, the RHS value is inserted to the array at the index specified by the beginning of the range.Removing items from the tail
Following syntax will not work:
a[-1..-2] = []This is because the range
-1..-2is an empty range(just like the range2..1).(-1..-2).to_a # => []In a valid range, the starting number should be smaller than the ending number. The number
-2is smaller than the number-1. Hence the range is written as-2..-1.(-2..-1).to_a # => [-2, -1]
— Josh Marshal . ( PS: I have modified the original quote by replacing the word Saying; at the beginning of the sentence; with the word Demanding.)
Here is my implementation for Conway’s game of life
Here is an example of polymorphic and STI models working together:
class User
include Mongoid::Document
embeds_many :phones, :as => :identifiable
embeds_many :emails, :as => :identifiable
embeds_many :social_profiles, :as => :identifiable
field :first_name, type: String
field :middle_name, type: String
field :last_name, type: String
field :email, type: String
field :password, type: String
end
class Identifier
include Mongoid::Document
embedded_in :identifiable, polymorphic: true
field :name, type: String # work, home, linked_in etc
field :valid, type: Boolean
field :validated_at, type: DateTime
end
class Email < Identifier
field :email, type: String
end
class Phone < Identifier
field :phone, type: String
end
class SocialProfile < Identifier
field :url, type: String
end
Prerequisites:
Steps:
- Download Win32 dynamic at 16 bits-per-pixel bundle. Do not use the 64 bit bundle.
- Install the bundle to a directory without spaces (e.g.,
C:\ImageMagick). Select the option for the header files during installation. Add ImageMagick exe directory to path(e.g.,
C:\imagemagick\ImageMagick-6.7.7-Q16). Read this article to find out how to setPATH.Check ImageMagick installation: Launch a new command shell and check the location of the identify command.
where identifyInstall the
rmagickgem ( notice the forward slash)gem install rmagick -- --with-opt-dir=C:/imagemagick/ImageMagick-6.7.7-Q16
Reference
The issue is dicussed at length at github. Here is a temporary fix:
Clone the git repo
git clone https://github.com/eventmachine/eventmachine.git
cd eventmachine/ext
Edit extconf.rb and paste the following lines at line 103
found = RbConfig::CONFIG.values_at("CFLAGS", "CPPFLAGS").any? { |v| v.include?("FD_SETSIZE") }
$defs.push "-DFD_SETSIZE=32767" unless found
Install the gem using the cloned+modified repo
cd ..
gem build eventmachine.gemspec
gem install eventmachine-1.0.0.rc.1.gem
Run the thin server in project directory
thin start

Potato Salad with Herbed Dijon Vinaigrette
Ingredients:
1 ½ to 2 lbs New potatoes, halved
¼ Cup Extra Virgin Olive Oil
1 tsp White Wine Vinegar
½...
Liz Brizzi ‘Escape Route’


New York-based artist Matt Wisniewski creates mixed media images that are beautifully refreshing. His digital art collages create a stunning fashion...

Kanu-flight by *Raphelt/Alexander Raphelt

Erick van Egeraat Office Tower by Erick van Egeraat
The tower has an innovative composition of being light at the bottom and heavy on top, achieve...

Trollwall Restaurant by Reiulf Ramstad Architects
