I wanted to post some interesting very few questions and answers on occasion of teachers day today!!!! Some the mandatory and interesting questions clubbed together in an interview Here I go... :). This is just not for jobs for your interest toooooo
1. Why Ruby on Rails?
Ans: There are lot of advantages of using ruby on rails(check the meaning of this in the book.)
- DRY Principal(Dont Repeat Yourself)
- Convention over Configuration
- Gems and Plugins
- Scaffolding
- Pure OOP Concept
- Rest Support
- Rack support
- Action Mailer
- Rpc support
- Rexml Support etc..
2. Explain about the programming language ruby?
Ans:Ruby is the brain child of a Japanese programmer Matz. He created Ruby. It is a cross platform object oriented language. It helps you in knowing what your code does in your application. With legacy code it gives you the power of administration and organization tasks. Being open source, it did go into great lengths of development.
.Ruby is a Dyanmic-type programming language and mainly referred to as Duck-typing.
3. Explain about ruby names?
Ans:Classes, variables, methods, constants and modules can be referred by ruby names. When you want to distinguish between various names you can specify that by the first character of the name. Some of the names are used as reserve words which should not be used for any other purpose. A name can be lowercase letter, upper case letter, number, or an underscore, make sure that you follow the name by name characters.
4. What is the Difference between Symbol and String?
Ans: Symbol are same like string but both behaviors is different based on object_id, memory and process time (cpu time) Strings are mutable , Symbols are immutable.But, testing two symbol values for equality (or non-equality) is faster than testing two string values for equality,
Mutable objects can be changed after assignment while immutable objects can only be overwritten.
For example:
p "string object jak".object_id #=> 22956070
p "string object jak".object_id #=> 22956030
p "string object jak".object_id #=> 22956090
p :symbol_object_jak.object_id #=> 247378
p :symbol_object_jak.object_id #=> 247378
p :symbol_object_jak.object_id #=> 247378
p " string object jak ".to_sym.object_id #=> 247518
p " string object jak ".to_sym.object_id #=> 247518
p " string object jak ".to_sym.object_id #=> 247518
p :symbol_object_jak.to_s.object_id #=> 22704460
p :symbol_object_jak.to_s.object_id #=> 22687010
p :symbol_object_jak.to_s.object_id #=> 21141310
Note : Each unique string value has an associated symbol
5. What is Session and Cookies?
Ans: Session: are used to store user information on the server side.
Cookies: are used to store information on the browser side or we can say client side
Example: Session : say session[:user] = “jyotsnac” it remains when the browser is not closed
6. Difference between render and redirect?
Ans:render example:
render :partial
render :new
It will render the template new.rhtml without calling or redirecting to the new action.
redirect example:
redirect_to :controller => ‘users’, :action => ‘new’
It forces the clients browser to request the new action.
7. What is the Difference between Static and Dynamic Scaffolding?
Ans: The Syntax of Static Scaffold is like this:
ruby script/generate scaffold User Comment
Where Comment is the model and User is your controller, So all n all static scaffold takes 2 parameter i.e your controller name and model name, whereas in dynamic scaffolding you have to define controller and model one by one.
8. How you run your Rails Application without creating database ?
Ans: You can run application by uncomment the line in environment.rb
Path => rootpath conf/ environment.rb
# Skip frameworks you're not going to use (only works if using vendor/rails)
config.frameworks -= [ :action_web_service, :action_mailer,:active_record ]
9. How to use sql db or mysql db. without defining it in the database.yml
Ans: You can use ActiveRecord anywhere!
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection({
:adapter => 'postgresql',
:user => 'foo',
:password => 'bar',
:database => 'whatever'
})
class Task <>
set_table_tame "a_legacy_thingie"
def utility_methods
update_attribute(:title, "yep")
end
end
Task.find(:first)
It’s ActiveRecord, you know what to do. Going wild:
ActiveRecord::Base.establish_connection(:adapter => "sqlite3",:dbfile => ":memory:")
ActiveRecord::Schema.define(:version => 1) do
create_table :posts do |t|
t.string :title
t.text :excerpt, :body
end
end
class Post <>
validates_presence_of :title
end
Post.create(:title => "A new post!")
Post.create(:title => "Another post",
:excerpt => "The excerpt is an excerpt.")
puts Post.count
10. What are helpers and how to use helpers in ROR?
Ans: Helpers (“view helpers”) are modules that provide methods which are automatically usable in your view. They provide shortcuts to commonly used display code and a way for you to keep the programming out of your views. The purpose of a helper is to simplify the view. It’s best if the view file (RHTML/RXML) is short and sweet, so you can see the structure of the output.
11. What is Active Record?
Ans: Active Record are like Object Relational Mapping(ORM), where classes are mapped to table , objects are mapped to columns and object attributes are mapped to data in the table
12. Ruby Support Single Inheritance/Multiple Inheritance or Both?
Ans: Ruby Supports only Single Inheritance.
You can achieve Multiple Inheritance through MIXIN concept means you achieve using module by including it with classes. A good blog to understand Mix-in concepts http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/
13. What is the naming conventions for methods that return a boolean result?
Ans: Methods that return a boolean result are typically named with a ending question mark. For example: def active? return true #just always returning true end
14. What is the naming conventions for methods that return a boolean result?
Ans: Methods that return a boolean result are typically named with a ending question mark. For example: def active? return true #just always returning true end
15. How do the following methods differ: @my_string.strip and @my_string.strip! ?
Ans: The strip! method modifies the variable directly. Calling strip (without the !) returns a copy of the variable with the modifications, the original variable is not altered.
16. What's the difference in scope for these two variables: @name and @@name?
Ans: @name is an instance variable and @@name is a class variable, where it is a single variable for all the instances of a class
17. What is the log that has to seen to check for an error in ruby rails?
Ans: Rails will report errors from Apache in log/apache.log and errors from the Ruby code in log/development.log. If you're having a problem, do have a look at what these logs are saying. On Unix and Mac OS X you may run tail -f log/development.log in a separate terminal to monitor your application's execution.
18. What is the use of global variable $ in Ruby?
Ans: A class variable starts with an @@ sign which is immediately followed by upper or lower case letter. You can also put some name characters after the letters which stand to be a pure optional. A class variable can be shared among all the objects of a class. A single copy of a class variable exists for each and every given class.
To write a global variable you start the variable with a $ sign which should be followed by a name character. Ruby defines a number of global variables which also include other punctuation characters such as $_ and $-k.
For example: If you declare one variable as global we can access any where, where as class variable visibility only in the class Example
class Test
def h
$a = 5
@b = 4
while $a > 0
puts $a
$a= $a - 1
end
end
end
test = Test.new
test.h
puts $a # 5
puts @b #nil
19. Default access specifier of default constructor in ruby?
Ans:Default access specifier is the same as the access specifier of the class. default constructor means the constructor with out arguments.
20. What is the use of super in ruby rails?
Ans: Ruby uses the super keyword to call the superclass implementation of the current method
21. What is the difference between nil and false in ruby?
Ans: False is a boolean datatype, Nil is not a data type it have object_id 4
22. How is class methods defined in Ruby?
Ans: A:def self.methodname
--------
--------
end
or
def classname.methodname
--------
--------
end
or
class >> self {
def methodname
end
}
23. How is object methods defined in Ruby?
Ans:
class jak
def method1
--------
--------
end
end
obj=jak.new
It is single object
def obj.object_method_one
--------
--------
end
obj.Send(object_method_every)
It will be created every for every object creation
24. What are the operators available in Ruby?
Ans: Something that’s used in an expression to manipulate objects such as + (plus), - (minus), * (multiply), and / (divide). You can also use operators to do comparisons,such as with <, >, and &&.
25. What are the looping structures available in Ruby?
Ans: for..in
untill..end
while..end
do..end
Note: You can also use each to iterate a array as loop not exactly like loop
26. What are the object-oriented programming features supported by Ruby?
Ans: Classes,Objects,Inheritance,Singleton methods,polymorphism(accomplished by over riding and overloading) are some oo concepts supported by ruby.
27. What is the scope of a local variable in Ruby?
Ans: A new scope for a local variable is introduced in the toplevel, a class (module) definition, a method defintion. In a procedure block a new scope is introduced but you can access to a local variable outside the block.
The scope in a block is special because a local variable should be localized in Thread and Proc objects.
28. How is an iterator handled in Ruby?
Ans: Iterator is handled using keyword 'each' in ruby.
For example
number=[1,2,3]
then we can use iterator as
number.each do |i|
puts i
end
Above prints the values of an array $no which is accomplished using iterator.
29. How is visibility of methods changed in Ruby?
Ans: By applying the access modifier : Public , Private and Protected acces Modifier
30. What is the use of load and require in Ruby?
Ans: A method that loads and processes the Ruby code from a separate file, including whatever classes, modules, methods, and constants are in that file into the current scope. load is similar, but rather than performing the inclusion operation once, it reprocesses the code every time load is called.
31. Explain about class libraries in ruby?
Ans: Ruby has a strong set of class libraries and it covers from a variety of domains such as thread programming, domains and data types. Also ruby is a new language and it also has additional libraries coming every day. Many of the new languages which do exist have huge libraries because of their age.
32. Explain about portability?
Ans: Ruby language can be ported to many platforms. Ruby programs can be ported to many platforms without any modification to the source code. This feature made the language very useful and highly used by many programmers worldwide. Some of the platforms used are DOS, UNIX, WINDOWS, etc.