What are the ways of executing a block from an iterator method?
What are the editors supported by ruby?
vim for editing
rubymine for general developing
Do not forget about TextMate :-)
What are the looping structures available in ruby?
and this one
10.times do {|x| puts x}
for loop while loop until loop do..end is not a loop(it is alternate form of defining a block)
Code
count = 0 while count < 5 do puts count count += 1 end for i in 0..4 do puts i end y = 4 until y < 0 do puts y y -= 1 end
What is a class instance variable?
there's no class instance variable but there is instance variable and class variable, or global variable @variable is shared in the defined instance
@@variable is shared for all instances of this class
and also you need attr_accessor to enable access from outside
There is nothing called Class instance variables but there are Class variables that are shared by all the instances of a Class. Every Class variable is declared preceded by @@.
Code
class SomeClass @@no_of_instances = 0 ..... ... .. end
super is used to call the parent classes initialize method from the childs initialize method. super has following forms of implementation. 1. Calling only the super means, call the parents initialize ...
Use the super keyword to call the superclass implementation of the current method.
Should we prefer ruby over PHP? If yes why?
Ruby is simple to use and flexible for object oriented and functional oriented
What are the operating systems supported by ruby?
Mac Os, Windows, Linux
Ruby is supported by windows and linux.
What are the rules and conventions to be followed in ruby for naming a method?
According to Martin Fowler, http://martinfowler.com/bliki/ClassInstanceVariable.html, A Class Instance Variable is a class-level variable that DOES NOT get inherited in any of the subclasses like a cl...
method should start with small letter.If the method name contain two words use underscore to separate the words
ex:get_name
What is the use of :var in ruby?
In Ruby the :var is used to define a symbol. A symbol ii ruby is created by adding 'colon' in-front of variable name .You can also create symbol by transforming a string to symbol using string#inte...
:var : Semicolon followed by an identifier tells us that var is symbol.Two strings with same content may belong to two totally different objetcs but if you convert these two strings to symbol, both of...
How does nil and false differ?
FALSE is a Boolean data type while NIL is not a data type.
In Ruby nil is an Object for NilClass,
You can directly check as nil.classit will return NilClass.
Where as false is an object for FalseClass.
Using two databases to a single application
How to use two databases to a single application?
This is for Ruby on Rails.1. Set the database at config/database.yml. ex mydb: adapter: mysql database: mydb socket: /tmp/mysql.sock &n...
What is the use of destructive method?
It is a method that alters the attribute of an object.myabcs = ['a', 'b', 'c']#myabcs is of type Array.myabcs.reverse!# this method altered the object.p myabcs#outputs:#["...
What is the use of GLobal variable $ in ruby?
A Global variable ($a) has a full scope in application. We can use any where in the application as you want.
If you declare one variable as global we can access any where, where as class variable visibility only in the class Exampleclass Testdef h $a = 5 @b = 4 while $a > 0puts $a$a= $a - 1endendendtes...
What are the operators available in ruby?
Don't forget the funky ones like a <=> b, which returns 1 if a > b, 0 if a = b and -1 if a < b.
Something thats 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 &&.
How is visibility of methods changed in ruby?
By applying the access modifier : Public , Private and Protected acces Modifier
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.
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.
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 variabl...
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.
How is class methods defined in ruby?
def self.methodname
--------
--------
end
or
def classname.methodname
--------
--------
end
when you can finish your code in one line, use {|x| ..... }
when you have multiple lines in a blocki, use
do |x|
....
end
Iterate and a block through an array :-
presidents = ["Ford", "Carter", "Reagan", "Bush1", "Clinton", "Bush2"]
presidents.each {|pres| puts pres}
OR
#The block neednt be on one line:
presidents = ["Ford", "Carter", "Reagan", "Bush1", "Clinton", "Bush2"]
presidents.each {
|pres|
puts pres
}