Created
November 25, 2012 08:22
-
-
Save sent-hil/4142823 to your computer and use it in GitHub Desktop.
Ruby job questions
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
http://blog.firsthand.ca/2010/08/questions-asked-in-ruby-on-rails-job.html | |
Q. What is polymorphism? | |
* Allows values of different data types to be handled using an uniform interface. | |
* 3 types: | |
* Overloading: Methods of same name exists in different class. | |
Ex: + method operates differently for integers and floats. | |
* Parametric: Methods of same name, but accept diff. parameters. | |
* Inclusion: The ability to redefine methods in inherited classes. | |
Q. What is overriding and overloading? | |
* Overriding: Ability to redefine a method that is inherited from parent class. | |
* Overloading: Ability to have two method of same names, but differing in args | |
or return type. (Not available in Ruby). | |
Q. What does a test suite contain? | |
* In rspec: describe, before, it, after | |
Q. What is a singleton method? | |
* Also known as anonymous, eigenclasses. | |
* Method that is defined for that object only. | |
* Defining a singleton method on one string object. | |
str = String.new | |
def str.hello | |
puts 'hello' | |
end | |
str.hello | |
The `hello` method is defined in an anonymous/singleton class, which is added | |
to class hierarchy between str and String. | |
* Can't instantiate `new` on singleton class, but you can call `super` from | |
there. | |
* Can't call Marshal.dump on singleton classes. | |
* When you use `extend` method on an object, the methods are placed into an | |
singleton class. | |
module Foo | |
def foo | |
"Hello World" | |
end | |
end | |
foobar = [] | |
foobar.extend(Foo) | |
foobar.singleton_methods # => ["foo"] | |
* Can open a singleton class directly using `<<` syntax. | |
foobar = [] | |
class << foobar | |
def foo | |
"Hello World!" | |
end | |
end | |
* Can also be defined with `instance_eval` | |
foobar = [] | |
foobar.instance_eval << EOT | |
def foo | |
"Hello World!" | |
end | |
EOT | |
* Ruby only supports instance methods. The class methods are actually instance | |
methods on the class. | |
class Foo | |
def self.one | |
end | |
end | |
Q: What is the difference between a block, lambda and proc? | |
* All three are different way to handle closure. | |
* Blocks - defined with do...end or {} (the latter has a tighter binding) | |
array = [1,2] | |
array.each do |e| | |
puts e | |
end | |
* Array class has `each` method which calls `yield` with one argument: e. | |
* Block is a proc that can't be saved and therefore an one time solution. | |
* Proc is an object and be treated, i.e. passed like any other object in args. | |
* & converts block to a Proc object. | |
* Procs are first class functions. | |
* Lambdas are like procs, except it checks the number or arguments passed. | |
* A proc return will stop the method and return the value provided, however | |
a lambda will return the value to the method and the let the method continue. | |
def proc_return | |
Proc.new { return "Proc.new" }.call | |
return "proc_return method finished | |
end | |
def lambda_return | |
lamdbda { return "lambda" }.call | |
return "lambda_return method finished" | |
end | |
puts proc_return | |
puts lambda_return | |
* Procs are drop in code snippets, not methods, while lambdas act like methods. | |
TODO: | |
First class functions | |
Closures | |
General OOP topics | |
How Ruby differs from other OOP languages | |
Sources: | |
http://en.kioskea.net/contents/poo/polymorp.php3 | |
http://www.devalot.com/articles/2008/09/ruby-singleton | |
http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment