Showing posts with label Ruby on Rails. Show all posts
Showing posts with label Ruby on Rails. Show all posts

Monday, March 30, 2009

Ruby Core intervew Questions

1. What is the use of load and require in Ruby?
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.
2. How is visibility of methods changed in Ruby?
By applying the access modifier : Public , Private and Protected acces Modifier
3. How is an iterator handled in Ruby?
Iterator is handled using keyword 'each' in ruby.
For example
$no=[1,2,3]
then we can use iterator as
$no.each do |l|
puts l
end
Above prints the values of an array $no which is accomplished using iterator.

4. What is the scope of a local variable in Ruby?
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.
while, until, and for are control structures and the scope is shared with the outside of these structures. loop is a method and the appended block introduces a new scope.
5. What are the object-oriented programming features supported by Ruby?
Classes,Objects,Inheritance,Singleton methods,polymorphism(accomplished by over riding and overloading) are some oo concepts supported by ruby. ...
6. What are the looping structures available in Ruby?
for..in
untill..end
while..end
do..end
7. What are the operators available in Ruby?
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 &&.
8. How is class methods defined in Ruby?
def self.methodname
--------
--------
end
or
def classname.methodname
--------
--------
end
9. How is class methods defined in Ruby?
A:def self.methodname
--------
--------
end
or
def classname.methodname
--------
--------
end
10. What is the difference between nil and false in ruby?
False is a boolean datatype Nil is not a data type
11. What is the log that has to seen to check for an error in ruby rails?
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.
12. What is the Install rail package?
There are several packages that you can download and install. The prebuilt Rails installer called Install rail which currently is only for Windows
13. Where does the start_tabnav gets informations for tabs rendering in ruby rail?
The main Symbol let the start_tabnav method know to look for a special MainTabnav class where all the magic happens.
14. What is the use of super in ruby rails?
Ruby uses the super keyword to call the superclass implementation of the current method
15. What is the log that has to seen to check for an error in ruby rails?
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.
16. What is the Install rail package?
There are several packages that you can download and install. The prebuilt Rails installer called Install rail which currently is only for Windows
17. Where does the start_tabnav gets informations for tabs rendering in ruby rail?
The main Symbol let the start_tabnav method know to look for a special MainTabnav class where all the magic happens
What is the use of global variable $ in Ruby?
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
What is the log that has to seen to check for an error in ruby rails?
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.
What's the difference in scope for these two variables: @name and @@name?
@name is an instance variable and @@name is a class variable
How do the following methods differ: @my_string.strip and @my_string.strip! ?
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.
What is the naming conventions for methods that return a boolean result?
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

Ruby on Rails FAQs

1. What is Ruby?
Ruby is a pure object-oriented programming language with a super clean syntax that makes programming elegant and fun. Ruby successfully combines Smalltalk's conceptual elegance, Python's ease of use and learning, and Perl's pragmatism. Ruby originated in Japan in the early 1990s, and has started to become popular worldwide in the past few years as more English language books and documentation have become available.
2. What is Rails?
Rails is an open source Ruby framework for developing database-backed web applications. What's special about that? There are dozens of frameworks out there and most of them have been around much longer than Rails. Why should you care about yet another framework? What would you think if I told you that you could develop a web application at least ten times faster with Rails than you could with a typical Java framework? You can--without making any sacrifices in the quality of your application! How is this possible? Part of the answer is in the Ruby programming language. Many things that are very simple to do in Ruby are not even possible in most other languages. Rails takes full advantage of this. The rest of the answer is in two of Rail's guiding principles: less software and convention over configuration. Less software means you write fewer lines of code to implement your application. Keeping your code small means faster development and fewer bugs, which makes your code easier to understand, maintain, and enhance. Very shortly, you will see how Rails cuts your code burden. Convention over configuration means an end to verbose XML configuration files--there aren't any in Rails! Instead of configuration files, a Rails application uses a few simple programming conventions that allow it to figure out everything through reflection and discovery. Your application code and your running database already contain everything that Rails needs to know!
3. How to capitalize UTF8 string without downcasing?
What is the best way to capitalize the first character of a UTF8 string? String#capitalize downcases the rest of the string which is unacceptable since there can be capitals in the middle of the string. ("see New York".capitalize => "See new york"). Just picking off the first byte and capitalizing that is also not an option since the first character can be a multibyte UTF-8 character. The only thing I can think of so far is either using Ruby 1.9 or the the Rails multibyte Chars library to split off the first character and then capitalize that and join it back together. Surely there must be a better way for such a common problem?
4. What is the naming conventions for methods that return a boolean result?
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
5. What are the object-oriented programming features supported by Ruby?
Classes,Objects,Inheritance,Singleton methods,polymorphism(accomplished by over riding and overloading) are some oo concepts supported by ruby. ...
6. What are the operating systems supported by Ruby?
Windows and linux operating systems are supported by the Ruby
7.Whats the difference between symbol and string?
Symbol refers to the same memory location where string generates a new id every time for eg.
STRING
irb(main):019:0> "ruby".object_id
=> 24095860
irb(main):020:0> "ruby".object_id
=> 24092310
irb(main):021:0> "ruby".object_id
=> 24088760
irb(main):022:0>
SYMBOL
irb(main):022:0> :ruby.object_id
=> 102978
irb(main):023:0> :ruby.object_id
=> 102978
irb(main):024:0> :ruby.object_id
=> 102978
irb(main):025:0> :ruby.object_id
=> 102978
irb(main):026:0>
8. How do you comment out a block of code?
Use =begin and =end. =begin def
my_commented_out_method end =end
You could use successive # signs, but that's just tedious: # # def my commented_out_method # end #
9. How would you create a new ruby rail application?
To create a new ruby rail application rails my_app cd my_app
10. What is the difference between nil and false in ruby?
False is a boolean datatype Nil is not a data type
11. What two delimiters are used for blocks?
Curly braces {...} and "do"..."end" Bonus: coding convention is to use curly braces if the code will fit on one line and "do"..."end" syntax if the block contains multiple lines.
12. What is the naming conventions for methods that return a boolean result?
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
13. Explain about the programming language ruby?
Ruby is the interpreted scripting language for quick and easy object-oriented programming. It has many features to process text files and to do system management tasks (as in Perl). It is simple, straight-forward, extensible, and portable. Oh, I need to mention, it's totally free, which means not only free of charge, but also freedom to use, copy, modify, and distribute it.
Features of Ruby
* Ruby has simple syntax, partially inspired by Eiffel and Ada.
* Ruby has exception handling features, like Java or Python, to make it easy to handle errors.
* Ruby's operators are syntax sugar for the methods. You can redefine them easily.
* Ruby is a complete, full, pure object oriented language: OOL. This means all data in Ruby is an object, in the sense of Smalltalk: no exceptions. Example: In Ruby, the number 1 is an instance of class Fixnum.
* Ruby's OO is carefully designed to be both complete and open for improvements. Example: Ruby has the ability to add methods to a class, or even to an instance during runtime. So, if needed, an instance of one class *can* behave differently from other instances of the same class.
* Ruby features single inheritance only, *on purpose*. But Ruby knows the concept of modules (called Categories in Objective-C). Modules are collections of methods. Every class can import a module and so gets all its methods for free. Some of us think that this is a much clearer way than multiple inheritance, which is complex, and not used very often compared with single inheritance (don't count C++ here, as it has often no other choice due to strong type checking!).
* Ruby features true closures. Not just unnamed function, but with present variable bindings.
* Ruby features blocks in its syntax (code surrounded by '{' ... '}' or 'do' ... 'end'). These blocks can be passed to methods, or converted into closures.
* Ruby features a true mark-and-sweep garbage collector. It works with all Ruby objects. You don't have to care about maintaining reference counts in extension libraries. This is better for your health. ;-)
* Writing C extensions in Ruby is easier than in Perl or Python, due partly to the garbage collector, and partly to the fine extension API. SWIG interface is also available.
* Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances of class Fixnum) and large integers (Bignum), but you need not worry over which one is used currently. If a value is small enough, an integer is a Fixnum, otherwise it is a Bignum. Conversion occurs automatically.
* Ruby needs no variable declarations. It uses simple naming conventions to denote the scope of variables. Examples: simple 'var' = local variable, '@var' = instance variable, '$var' = global variable. So it is also not necessary to use a tiresome 'self.' prepended to every instance member.
* Ruby can load extension libraries dynamically if an OS allows.
* Ruby features OS independent threading. Thus, for all platforms on which Ruby runs, you also have multithreading, regardless of if the OS supports it or not, even on MS-DOS! ;-)
* Ruby is highly portable: it is developed mostly on Linux, but works on many types of UNIX, DOS, Windows 95/98/Me/NT/2000/XP, MacOS, BeOS, OS/2, etc.
14. Why Ruby on Rails?
1)for fast developing websites
2)convention over configuration
3)scaffolding
4)pure Object oriented concepts
5)less coding
6)easy understanding of coding
7)follows MVC architecture
8)using library and gem files