-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruby.html
More file actions
80 lines (47 loc) · 8.49 KB
/
ruby.html
File metadata and controls
80 lines (47 loc) · 8.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
---
layout: layout
title: "Ruby | Eric Farkas"
---
<h3>Notes on Ruby and Rails</h3>
<br/>
<p><b>What is rubygems?</b><br/>
A gem manager. Gems are packaged Ruby applications or libraries.</p>
<p><b>What is a Symbol?</b><br/> Symbols are used to represent names and strings, and they look like this: <code>:foo</code>.</p>
<p><b>What is the difference between a Symbol and String?</b><br/>
Symbols are immutable and each instance of a symbol with the same name will have the same memory address (better for performance since each String instance gets it's own memory address)</p>
<p><b>What is the purpose of yield?</b><br/>
Give control back to the calling block</p>
<p><b>How do you define class variables?</b><br/> Like this: <code>@@foo</code>.</p>
<p><b>How do you define instance variables?</b><br/> Like this <code>@bar</code>.</p>
<p><b>How do you define global variables?</b><br/> Like this: <code>$foo</code>.</p>
<p><b>How can you dynamically define a method body?</b><br/> <a href="http://yehudakatz.com/2009/11/15/metaprogramming-in-ruby-its-all-about-the-self/">http://yehudakatz.com/2009/11/15/metaprogramming-in-ruby-its-all-about-the-self/</a></p>
<p><b>What is a Range?</b><br/> A Range is a class that holds a range of numbers. <code>(1..10)</code> is all the numbers between 1 and 10, including 1 and 10; <code>(1...10)</code> is all the numbers between 1 and 10, excluding 1 and 10</p>
<p><b>How can you implement method overloading?</b><br/> Make the method accepts <code>args*</code> as parameter(s), use logic to delegate based on # or type of arguments. Put <code>args*</code> at the end of the parameter in cases where you want to enforce a minimum amount of parameters.</p>
<p><b>What is the difference between <code>&&</code> and <code>and</code> operators?</b><br/> <code>&&</code> is true if what you're comparing are both non-zero, <code>and</code> is true if both are true.</p>
<p><b>What is the convention for using <code>!</code> at the end of a method name?</b><br/> Denotes that there is a potentially dangerous or unexpected side-effect to the method; usually that it changes the object you're working on in-place.</p>
<p><b>What is a module?</b><br/> A class that can have no instances or subclasses; mechanism for namespaces (<a href="http://www.rubyist.net/~slagell/ruby/modules.html">http://www.rubyist.net/~slagell/ruby/modules.html</a>).</p>
<p><b>What is mixin?</b><br/>
Class or module that will be "included" in or "mixed in" to another class (which brings with it all it's variables and methods); a way of getting around Ruby's lack of multiple inheritance (<a href="http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html">http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html</a>).
<p><b>How will you implement a singleton pattern?</b><br/> <a href="http://dalibornasevic.com/posts/9-ruby-singleton-pattern-again">http://dalibornasevic.com/posts/9-ruby-singleton-pattern-again</a></p>
<p><b>How will you implement a observer pattern?</b><br/> <a href="http://khelll.com/blog/ruby/observer-and-singleton-design-patterns-in-ruby/">http://khelll.com/blog/ruby/observer-and-singleton-design-patterns-in-ruby/</a></p>
<p><b>How can you define a constant?</b><br/> Like this: <code>FOO = "some value"</code>.</p>
<p><b>How can you define a custom Exception?</b><br/> Create a class that inherits from <code>StandardError</code>, then "raise" that exception.</p>
<p><b>Why is the id of nil 4 in Ruby?</b><br/>
<a href="http://blog.bigbinary.com/2008/06/23/why-the-id-of-nil-is-4-in-ruby.html">http://blog.bigbinary.com/2008/06/23/why-the-id-of-nil-is-4-in-ruby.html</a></p>
<p><b>What is the default access modifier (public/protected/private) for a method?</b><br/> <code>public</code></p>
<p><b>How can you call the base class method from inside of its overriden method?</b><br/> <code>super()</code></p>
<p><b>What's the difference between include and extend?</b><br/> <code>include</code> brings the methods of the included class into an instance of the including class. <code>extend</code> makes them available to the class itself. You can use <code>base.extend(ClassMethods)</code> to get both class and instance methods in a subclass (<a href="http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ http://rubyquicktips.com/post/1133877859/include-vs-extend">http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/</a>, <a href="http://rubyquicktips.com/post/1133877859/include-vs-extend">http://rubyquicktips.com/post/1133877859/include-vs-extend</a>).</p>
<p><b>What's the difference between include and require?</b><br/> <code>require</code> "runs the required" file, <code>include</code> adds functionality to the current module.</p>
<p><b>What is a class?</b><br/> A class holds data, and has methods that interact with that data.</p>
<p><b>What is a module? Can you tell me the difference between classes and modules?</b><br/> A module is a class that cannot be instantiated.</p>
<p><b>There are three ways to invoke a method in ruby. Can you give me at least two?</b><br/> The dot operator, as in <code>foo.some_method</code>, the <code>Object#send</code> method, or <code>method(:foo).call</code>.</p>
<p><b>Can you tell me the three levels of method access control for classes and modules? What do they imply about the method?</b><br/> All methods, no matter the access control, can be accessed within the class. <code>public</code> methods enforce no access control - they can be called in any scope. <code>protected</code> methods are only accessible to other objects of the same class. <code>private</code> methods are only accessible within the context of the current object.</p>
<p><b>What is a Proc?</b><br/> Anonymous method that can be passed around as a variable, variables inside remain in scope. Lamdbas are similar but need to be explicitly declared.</p>
<p><b>What's the difference between a Proc and a lamba?</b><br/> Lambdas are more explicit about parameter checking. See <a href="http://stackoverflow.com/questions/1740046/whats-the-difference-between-a-proc-and-a-lambda-in-ruby">http://stackoverflow.com/questions/1740046/whats-the-difference-between-a-proc-and-a-lambda-in-ruby</a></p>
<p><b>Talk about method_missing.</b><br/> With <code>method_missing</code> you can catch calls to methods that don't exist, and dynamically call them. ActiveRecord uses this technique to build dynamic finders with <code>find_by</code>.</p>
<p><b>Talk about closures.</b><br/>A closure is basically a function/method that has the following two properties: You can pass it around like an object (to be called later), It remembers the values of all the variables that were in scope when the function was created. It is then able to access those variables when it is called even though they may no longer be in scope. <a href="http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/">http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/</a></p>
<p><b>Ruby has a unique approach to the multiple inheritance problem. What are the strengths and weaknesses of the Ruby solution?</b><br/>
Ruby's answer to the multiple inheritance problem is not, strictly speaking, multiple inheritance at all, but rather the use of modules as "mix-ins". Mix-ins allow one class to be "mixed into" another class, thereby bringing all of it's instance methods into the new class. You can mix-in as many modules as you need in your class. By doing this, you are able to easily leverage the functionality of any existing mix-in in your new class. The strength of this approach is that you avoid the potential problem of inheriting from two classes who themselves inherit from one base class, and the namespace collisions that can ensue (commonly referred to as the "diamond problem"). If you are careful and attentive to the order in which you "include" modules in your class, you will always know which version of a method will be called from a mixed-in module. Another strength of this approach is perhaps more abstract, but in modeling your classes, mix-ins allow you think of your class as "having" certain behavior, instead of "being" a certain thing. My class "has" Comparable functionality, but is not primarily a Comparable child class.<br/>
One of the weaknesses of this approach is that it's easy for your classes to become huge in a hurry before you write any code. Some modules, in Rails core for example, already contain hundreds of methods. However, I think with attentive planning, this can be avoided.<br/>
<h3>See Also</h3>
<a href="https://github.com/styleguide/ruby">GitHub Ruby Style Guide</a>