...String, Integer, Float, Boolean, Array, Hash Control flow: if, each, case, break... Functions: def, return, implicit return in methods and blocks Errors, raise and rescue Classes and inheritance The difference...
...addresses.size # => 3 Now write a method AddressBook#search that takes a query string and returns an array of Contact objects that match the given word in any of their properties...
...What are "synchronous" or "blocking" APIs versus "asynchronous" APIs? Why does fetch() not simply return the response from the server? Why is asynchronous code such a big issue in JavaScript...
...Stub function that resolves after 100 milliseconds, could be an API call function backgroundTask() { return new Promise((resolve) => setTimeout(resolve, 100)) } init() What happens when you remove the await of...
...an expression like User.where(email: 'foo@bar.com') does not make an SQL query. It simply returns a scope object for further chaining with #where, #order, etc. Scopes will make an SQL...
...A scope like User.where(email: 'foo@bar.com') does not make an SQL query. It simply returns a scope object for further chaining with #where, #order, etc. It can be confusing that...
...named groups in Regex Exercises Find words Write a method second_words(string) that returns the second word of every sentence in the given string. Tip You can generate paragraphs...
...accessor sit on the same line. The superclass may be optional. ClassScanner#superclass should return nil in that case. Info In practice we would never parse Ruby code like this...
...redirect_to @movie # else # render 'new' # end render plain: params.inspect end Note that #inspect returns a human-readable representation of an object. All Ruby objects implement this. Submit the new...
...classic "unit tests" where you instantiate an object, call a method and observe its return value or side effects. Your MovieDB already uses the gem rspec-rails which has many...
If the given ID was not found, a redirect to /movies is returned. The HTTP status 307 is used for the redirect. The movies index shows a flash...
...The more tests you already have, the less useful an additional test becomes ("Diminishing returns"). Tests sometimes fail even though the code is fine, especially when the test is coupled...
...the component. Use Jasmine spies to mock the window.fetch() method, inspect its calls and return a promise for a Response...
...in a central place makes your code DRY. E.g. Movie.human_attribute_name(:title) will return the title translation from activerecord.attributes.movie.title. Browse through the locale files of a recent sample app...
Class Rectangle with properties width and height. It offers an area method that returns the product of width and height. Write an average function that takes an arbitrary number...
...can temporarily suspend a running vim editor, take a look around the system, then return to your vim editor. File ownership Understand access rights and ownership of files:
The class should cache its results so subsequent calls for the same URLs return the HTML size instantly, without making an additional HTTP request. Write two versions of WebsiteSizer...
...part of the implementation that lives in Javascript, implement a Search.match(query) method that returns a promise. You should be able to use it like this: var query = // ...
...API so the client requests a JSON document instead of a HTML snippet. The returned JSON could look like this: { 'results': [ { 'title': 'Sunshine', 'year': 2007, 'detailsURL': '/movies/4012' }, { 'title': 'Before Sunset...
Best results in other decks
An association defined with has_many :through will return the same record multiple times if multiple join models for the same record exist (a n:m relation). To prevent this...
invoice_id product_id 11 1001 55 12 1001 55 Now #products will return the product with ID #55 two times for this invoice: invoice = Invoice.find(1001) invoice.products # => [# , # ]
...IN (?)", excluded_ids) When the exclusion list is empty, you would expect this to return all records. However, this is not what happens: # Broken example User.where("id NOT IN (?)", []).to...
...SELECT `users`.* FROM `users` WHERE (id NOT IN (NULL)) Passing an empty exclusion list returns no records at all! See below for better implementations. Rails 4+ Use the .not method...