server question and answers
1.difference between application server and web server
ruby questions and answers
1.difference between module an class
You might also use a module when you have shared methods across multiple apps.
¦ class ¦ module ¦
¦---------------+------------------------------------+-----------------------------------------------------------------------¦
¦ instantiation ¦ can be instantiated ¦ can't be ¦
¦ usage ¦ object creation ¦ mixin facility. provide a namespace. ¦
¦ superclass ¦ module ¦ object ¦
¦ consists of ¦ methods, constants and variables ¦ methods, constants and classes ¦
¦ methods ¦ class methods, instance methods ¦ module methods, instance methods ¦
¦ inheritance ¦ inherits behavior and can ¦ No inheritance ¦
¦ ¦ be base for inheritance ¦ ¦
¦ inclusion ¦ can't be included ¦ can be included in classes/modules by using the include command ¦
¦ ¦ ¦ (includes all instance methods as instance methods in class/module) ¦
¦ extension ¦ can not extend with extend command ¦ module can extend instance by using extend command (extends given ¦
¦ ¦ (only with inheritance) ¦ instance with singleton methods from module) ¦
+----------------------------------------------------------------------------------------------------------------------------+
Classes are simply like Java classes.
Modules are like Java static classes. Think about Math class in Java. You don't instantiate it, and you reuse the methods in the static class (eg. Math.random()).
2.return types in ruby method
1. default return last value
2. explicitly value can also be returned.
3. only one return statement can be used
4. with one return statement multiple values can be sent.
3.class level methods and object methods
(www.railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/)
Class and Instance Methods in Ruby
Class methods are methods that are called on a class and instance methods are methods that are called on an instance of a class. Here is a quick example and then we’ll go into a bit more detail.
class Foo
def self.bar
puts 'class method'
end
def baz
puts 'instance method'
end
end
Foo.bar # => "class method"
Foo.baz # => NoMethodError: undefined method ‘baz’ for Foo:Class
Foo.new.baz # => instance method
Foo.new.bar # => NoMethodError: undefined method ‘bar’ for #<Foo:0x1e820>
See the difference? bar is a class method, so calling bar on the Foo class works fine. baz is an instance method, so calling baz on the Foo class raises a NoMethodError. Then, on the next couple lines, we call both methods on an instance of Foo (Foo.new).
Now that we have a base, let’s show a few of the ways you can create class and instance methods and examples of what they would be used for.
Class Methods
Ruby is very flexible and as such it allows several ways to define a class method. The following is a sample of the most commonly used ways.
# Way 1
class Foo
def self.bar
puts 'class method'
end
end
Foo.bar # "class method"
# Way 2
class Foo
class << self
def bar
puts 'class method'
end
end
end
Foo.bar # "class method"
# Way 3
class Foo; end
def Foo.bar
puts 'class method'
end
Foo.bar # "class method"
The first way is my preference. When I see self.method_name it is immediately apparent to me that this is a class method. A lot of people use way #2 and it is pretty heavily used in Rails.
There is nothing wrong with it, but when you have a class with a lot of class methods in a class << self block, it can be hard to tell if the method is a class or instance method because it is defined the same (def bar). If this doesn’t make sense, feel free to use it for a while and you’ll probably run into what I’m talking about.
Way 3 is not that common as far as I have seen and is more often a way to quickly add methods on the fly to a class. These are not the only three ways to define class methods, but they seem to be the ones that I see the most.
So when would you use a class method? Class methods are for anything that does not deal with an individual instance of a class. ActiveRecord::Base#find is one example. If you look in ActiveRecord::Base, you’ll see something like this:
module ActiveRecord
class Base
# some stuff
class << self
def find(...)
# blah
end
end
end
end
Looks familiar, eh? Some other uses of class methods in Rails are validations and associations in ActiveRecord and before/after/around filters in ActionPack. The way this works is something like this (simplified for clarity):
module ActiveRecord
class Base
def self.validates_presence_of(...)
# make sure present
end
end
end
class Foo < ActiveRecord::Base
validates_presence_of :bar
end
When you say validates_presence_of, the class method in AR::Base is what gets called.
Instance Methods
Enough about class methods, lets move on. Instance methods are a bit more simple. Here are a few common ways that instance methods are defined.
# Way 1
class Foo
def baz
puts 'instance method'
end
end
Foo.new.baz # "instance method"
# Way 2
class Foo
attr_accessor :baz
end
foo = Foo.new
foo.baz = 'instance method'
puts foo.baz
# Way 3
class Foo; end
foo = Foo.new
def foo.bar
puts 'instance method'
end
Foo.new.baz # "instance method"
The key difference is instance methods only work with an instance and thus you have to create a new instance to use them (Foo.new). Again, there are more ways to define instance methods than this, especially if you look into meta programming.
So what are some examples uses of instance methods in Rails, to give you a better idea? Ever do a find in a destroy action and then call destroy on the found instance? destroy is an instance method.
class FoosController < ActionController
def destroy
foo = Foo.find(params[:id])
foo.destroy
redirect_to foos_url
end
end
So are save and update_attributes, which you have definitely used before if you’ve done any Rails.
foo = Foo.new(:title => 'Bar')
foo.save # is an instance method
4.how to tranafer values between methods
(http://praaveenvr.blogspot.in/2014/03/ruby-on-rails-how-to-pass-variable.html)
If you want to share the value across the methods of a same controller instance then, declare an instance variable:
class BarsController < UsersController
before_filter :init_foo_list
def method1
render :method2
end
def method2
@foo_list.each do | item|
# do something
end
end
def init_foo_list
@foo_list ||= ['Money', 'Animals', 'Ummagumma']
end
end
If you want to share the value across two controllers withn a session, then:
class BarsController < UsersController
before_filter :init_foo_list
def method1
render :controller => "FoosController",
:action => "method2"
end
def init_foo_list
params[:shared_param__] ||= ['Money', 'Animals', 'Ummagumma']
end
end
class FoosController < UsersController
def method2
params[:shared_param__].eachdo | item|
# do something
end
end 2.return types
end
5.Abstract classes & Interfaces
There is no concept of abstract class and interface in ruby.
Another way to look at this issue is to think
of static typing as working like an aristocracy: Statically typed languages are constantly asking about your parent or grandparent, or perhaps, in the case of Java-style interfaces, your aunts and uncles. In a statically typed language, an objects family tree matters deeply. Dynamically typed languages, by contrast, are meritocracies:Types, Safety, and Flexibility
They are concerned with which methods you have, rather than where those methods came from. Dynamically typed languages rarely ask about an objects ancestry; instead, they simply say, I dont care who you are related to, Mac. All I want to know is what you can do.
6.why ruby doesn't support multiple inheritance?
7. Multiple inheritance in ruby
Both Java and Ruby doesn't support multiple inheritance. Where in Ruby we can achieve multiple inheritance by including multiple modules. this concept is called mixin.
8.keywords in ruby
The keywords are not objects but defined in the parser which can be found in parse.y in the Ruby source. Here's the relevant part from that file:
reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__ | keyword_BEGIN | keyword_END | keyword_alias | keyword_and | keyword_begin | keyword_break | keyword_case | keyword_class | keyword_def | keyword_defined | keyword_do | keyword_else | keyword_elsif | keyword_end | keyword_ensure | keyword_false | keyword_for | keyword_in | keyword_module | keyword_next | keyword_nil | keyword_not | keyword_or | keyword_redo | keyword_rescue | keyword_retry | keyword_return | keyword_self | keyword_super | keyword_then | keyword_true | keyword_undef | keyword_when | keyword_yield | keyword_if | keyword_unless | keyword_while | keyword_until ;
9.difference between each and map
each simply iterates over the given enumerable, running the block for each value. It discards the return value of the block, and each simply returns the original object it was called on:
[1, 2, 3].each do |i| i + 1 end # => [1, 2, 3]
This is simply a nicer, more universal way of doing a traditional iterating for loop, and each is muchpreferred over for loops in Ruby (in fact, I don't think I've ever used a for loop in Ruby).
map, however, sets the current element being iterated over to the return value of the block, and then returns a new object with those changes:
[1, 2, 3].map do |i| i + 1 end # => [2, 3, 4]
So it "maps" each element to a new one using the block given, hence the name "map".
10.mixins
1.Inside module creating object methods and this module is inherited(multiple inherited) by other classes.
2.So this module is available as class to the other class.
3. Module allowing other class to access its object method.
4. This is the reason behind the name 'mixins'
5.mixture of module,class,object method
11.Difference between render and redirect_to
(http://stackoverflow.com/questions/7493767/are-redirect-to-and-render-exchangeable)
12.difference between require an include
(http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/)
The require statement is similar to the include statement of C and C++ and the import statement of Java. If a third program wants to use any defined module, it can simply load the module files using the Ruby require statement:
ex:
require "Week"
class Decade
include Week no_of_yrs=10 def no_of_months puts Week::FIRST_DAY number=10*12 puts number end
end
d1=Decade.new puts Week::FIRST_DAY Week.weeks_in_month Week.weeks_in_year d1.no_of_months
13.module inside the class an class inside the module
14.relationships in sql
15.relationships in mongodb
16.difference between mongodb and mongoid/mango mapper
17. Agile development
1.pair programming
2.knowledge transfer
3.code re-factoring
4.daily stand up meets
5.no overtime
6.Continuous integration
7.story based or module based work(some times order of work/task execution)
8.User stories and users choosing a deck for next release
9.Small releases
10.Coding standards
11.Continuous iterations
12.test driven development
13.extreme programming
14.Tests first when find bugs
15. UML based approach
Scrum: A key principle of Scrum is its recognition that during a project the customers can change their minds about what they want and need (often called requirements churn), and that unpredictable challenges cannot be easily addressed in a traditional predictive or planned manner. As such, Scrum adopts an empirical approach—accepting that the problem cannot be fully understood or defined, focusing instead on maximizing the team's ability to deliver quickly and respond to emerging requirements.
the top 10 most effective agile practices (as reported by respondents) were:
Continuous Integration
Daily Stand Up Meeting
Developer TDD
Iteration Planning
Code Refactoring
Retrospectives
Pair Programming
Active Stakeholder Participation
Potentially Shippable Software
Burndown Tracking
Nobody used pair programming except in panic situations (major bug fix in zero time), managers still just don't buy into the idea at all - and I'm talking AT ALL, which is a shame as I quite like it. User stories and users choosing a deck for next release - doesn't really work unless the users really really buy in, which I've not seen yet. Users in my area always say that everything is of top importance, they cannot live without any of it. I personally just rephrase into "what order should I personally work on these tasks in your opinion?".
Test driven dev - very little of this happens, but a lot of unit tests get written (after the code does though), so a near miss imho
Continuous integration - this is highly dependent on the team, in last 5 years all my teams had it, but it often lapsed (broken build) for days/weeks at a time before it got attention. A lot of people still don't buy into this.
Refactoring often - this is actually getting some serious buy-in. Refactoring is a skill that if you don't have is likely to be a serious problem.
Small releases - this (in my work) is generally the norm anyway, although probably being done
Coding standards - yes
Collective code ownership - blame is still pretty much rife, and often a "bad" module never really gets fixed cos the coder that produced it just fixes and fixes it till it "works".
No overtime - nearly, but highly dependent on the team lead - I've seen the worst stuff (death marches) going on within a few feet of my team...
Tests first when find bugs - this happens in my experience. Is a very good thing.
18. ORM
ORM is Object Relational Mapper. It means you don't have to manually call the database yourself; the ORM handles it for you.
Ruby on Rails uses one called ActiveRecord, and it's a really good one.
ORM allows you to do things such as:
User.find(50).contacts
Instead of manually writing a SELECT statement with JOINs, WHEREs, etc.
ex : JAVA
(http://stackoverflow.com/questions/7493767/are-redirect-to-and-render-exchangeable)
19. when,where to use object,class variables?
http://rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/45-more-classes/lessons/113-class-variables
class Planet
@@planets_count = 0
def initialize(name)
@name = name
@@planets_count += 1
end
def self.planets_count
@@planets_count
end
end
Planet.new("earth"); Planet.new("uranus")
p Planet.planets_count
stdout:
2
class ApplicationConfiguration
@@configuration = {}
def self.set(property, value)
@@configuration[property] = value
end
def self.get(property)
@@configuration[property]
end
end
class ERPApplicationConfiguration < ApplicationConfiguration
end
class WebApplicationConfiguration < ApplicationConfiguration
end
ERPApplicationConfiguration.set("name", "ERP Application")
WebApplicationConfiguration.set("name", "Web Application")
p ERPApplicationConfiguration.get("name")
p WebApplicationConfiguration.get("name")
p ApplicationConfiguration.get("name")
stdout:
"Web Application"
"Web Application"
"Web Application"
class Foo
@foo_count = 0
def self.increment_counter
@foo_count += 1
end
def self.current_count
@foo_count
end
end
class Bar < Foo
@foo_count = 100
end
Foo.increment_counter
Bar.increment_counter
p Foo.current_count
p Bar.current_count
stdout:
1
101
20. modules
a.Modules provide a namespace
b.Modules implement the mixin facility.
namespace :
module Trig
PI = 3.141592654
def Trig.sin(x)
# ..
end
def Trig.cos(x)
# ..
end
end
module Action
VERY_BAD = 0
BAD = 1
def Action.sin(badness)
# ...
end
end
require "trig" require "action"
y = Trig.sin(Trig::PI/4) wrongdoing = Action.sin(Action::VERY_BAD)
mixin
21.Why Ruby Does Not Support Method Overloading?
There may be only one method with given name in Ruby class. If several methods with the same name defined in the class - the latest overwrites previous definitions.
However, you may emulate methods overloading like this:
class Person
def print_details(*args)
case args.size
when 1 "Hey My Name is #{args[0]}" when 2 "Hey My Name is #{args[0]} and #{args[1]}" end
end end
22. access specifiers in ruby
23.difference symbol ans string
Rails questions and answers
1.Difference between plugin and gem
2.Defference between gem in vender/gems and gems
3.rake routes GET POST DELETE (HTTP request method)?
(HTTP request method)?
GET
POST
PATCH ( previous versions of Rails used PUT in place of PATCH)
DELETE
The hypertext transfer protocol (HTTP) defines the basic operations GET, POST, PATCH, andDELETE. These refer to operations between a client computer (typically running a web browser such as Firefox or Safari) and a server (typically running a web server such as Apache or Nginx). (It’s important to understand that, when developing Rails applications on a local computer, the client and server are the same physical machine, but in general they are different.) An emphasis on HTTP verbs is typical of web frameworks (including Rails) influenced by the REST architecture, which we saw briefly inChapter 2 and will start learning about more in Chapter 7.
GET is the most common HTTP operation, used for reading data on the web; it just means “get a page”, and every time you visit a site like google.com or wikipedia.org your browser is submitting aGET request. POST is the next most common operation; it is the request sent by your browser when you submit a form. In Rails applications, POST requests are typically used for creating things
4.difference between erb and html, purpose of erb?
ERb is the primary template system for including dynamic content in web pages
5.The asset pipeline(refer http://ruby.railstutorial.org/ chapter-5)
Manifest files
Preprocessor engines
6.alternative for javascript,css,html (coffee script,sass,erb)
7.things to follow in ROR
(reference 12factor.net)
a. MVC
b. when to use what (javascript,css,html or coffeescript,sass,erb)
c. separation between styles,script and erb.
d. time delay between between activerecord and mysql(type of db we use).
e.should follow http request method(get,post,patch,delete)
8. x.min.js will make any sense because pre-processor engines default remove unwanted spaces.
9.Difference between .js and min.js
jquery.js = Pretty and easy to read :) Read this one.
jquery.min.js = Looks like jibberish! But has a smaller file size. Put this one on your site.
Both are the same in functionality. The difference is only in whether it's formatted nicely for readability or compactly for smaller file size.
Specifically, the second one is minified, a process which involves removing unnecessary whitespace and shortening variable names. Both contribute to making the code much harder to read: the removal of whitespace removes line breaks and spaces messing up the formatting, and the shortening of variable names (including some function names) replaces the original variable names with meaningless letters.
All this is done in such a way that it doesn't affect the way the code behaves when run, in any way. Notably, the replacement/shortening of variable and function names is only done to names that appear in a local scope where it won't interfere with any other code in other scripts.
10.unobtrusive JS
The benefits of unobtrusive JavaScript won’t really appear until our web application has a lot more JavaScript in it as then the advantages of having all of the scripts in a separate file and being able to remove duplication begin to show themselves.
11. difference between include, load, require, extend
12. metaprogramming
13. difference bwtwen render and redirect
14 about mongo db
15. sql database modeling
16. about scrum
17. custom routes(http://guides.rubyonrails.org/routing.html#naming-routes)
18. ORM
19.What Is the Difference Between a Block, a Proc, and a Lambda in Ruby?
20.http://awaxman11.github.io/blog/2013/08/05/what-is-the-difference-between-a-block/
No comments:
Post a Comment