The best place for learning ruby is to get started with the programming-ruby. It fairly covers the important bits in a very readable language. Here are a few quick questions on ruby:
- What is rubygems?
- What is a Symbol?
- What is the difference between a Symbol and String?
- What is the purpose of yield?
- How do you define class variables?
- How do you define instance variables?
- How do you define global variables?
- How can you dynamically define a method body?
- What is a Range?
- How can you implement method overloading?
- What is the difference between ‘&&’ and ‘and’ operators?
- What is the convention for using ‘!’ at the end of a method name?
- What is a module?
- What is mixin?
- How will you implement a singleton pattern?
- How will you implement a observer pattern?
- How can you define a constant?
- How can you define a custom Exception?
- How can you fire a method when a module is included inside a class?
- What is the default access modifier (public/protected/private) for a method?
- How can you call the base class method from inside of its overriden method?
ANSWERS:
1.What is rubygems?
Ans. RubyGem is a software package, commonly called a “gem”. Gems contain a packaged Ruby application or library. The RubyGems software itself allows you to easily download, install, and manipulate gems on your system.
Each gem has a name, version, and platform. For example, the rake gem has a 0.8.7
version. Rake’s platform is ruby
, which means it works on any platform Ruby runs on. Other platforms include java
(like nokogiri) and mswin32
(like sqlite-ruby).
Gems can be used to extend or modify functionality within a Ruby application. Commonly, they’re used to split out reusable functionality that others can use in their applications as well. Some gems also provide command line utilities to help automate tasks and speed up your work. As of Ruby 1.9.2, RubyGems is included when you install the programming language, so gems are both ubiquitous and extremely useful. If you’re using an earlier version of Ruby, it’s simple to install RubyGems as an addon.
2.What is the difference between a 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.
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
And also it will differ by process time
For example:
Testing two symbol values for equality (or non-equality) is faster than testing two string values for equality,
Note : Each unique string value has an associated symbol
3.What is the purpose of yield?
Ans: You invoke a block by using the yield statement.
Let.s look at an example of the yield statement:
#!/usr/bin/ruby
def test
puts "You are in the method"
yield
puts "You are again back to the method"
yield
end
test {puts "You are in the block"}
|
This will produce following result:
You are in the method
You are in the block
You are again back to the method
You are in the block
|
You also can pass parameters with the yield statement. Here is an example:
#!/usr/bin/ruby
def test
yield 5
puts "You are in the method test"
yield 100
end
test {|i| puts "You are in the block #{i}"}
|
This will produce following result:
You are in the block 5
You are in the method test
You are in the block 100
|
Here the yield statement is written followed by parameters. You can even pass more than one parameter. In the block, you place a variable between two vertical lines (||) to accept the parameters. Therefore, in the preceding code, the yield 5 statement passes the value 5 as a parameter to the test block.
4.How do you define class variables?
Ans: Class variables begin with @@ and must be initialized before they can be used in method definitions.
Referencing an uninitialized class variable produces an error. Class variables are shared among descendants of the class or module in which the class variables are defined.
Overriding class variables produce warnings with the -w option.
Here is an example showing usage of class variable:
#!/usr/bin/ruby
class Customer
@@no_of_customers=0
def initialize(id, name, addr)
@cust_id=id
@cust_name=name
@cust_addr=addr
end
def display_details()
puts "Customer id #@cust_id"
puts "Customer name #@cust_name"
puts "Customer address #@cust_addr"
end
def total_no_of_customers()
@@no_of_customers += 1
puts "Total number of customers: #@@no_of_customers"
end
end
# Create Objects
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")
# Call Methods
cust1.total_no_of_customers()
cust2.total_no_of_customers()
|
Here @@no_of_customers is a class variable. This will produce following result:
Total number of customers: 1
Total number of customers: 2
5.How do you define instance variables?
Ans: Instance variables begin with @. Uninitialized instance variables have the value nil and produce warnings with the -w option.
Here is an example showing usage of Instance Variables.
#!/usr/bin/ruby
class Customer
def initialize(id, name, addr)
@cust_id=id
@cust_name=name
@cust_addr=addr
end
def display_details()
puts "Customer id #@cust_id"
puts "Customer name #@cust_name"
puts "Customer address #@cust_addr"
end
end
# Create Objects
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")
# Call Methods
cust1.display_details()
cust2.display_details()
|
Here @cust_id, @cust_name and @cust_addr are instance variables. This will produce following result:
Customer id 1
Customer name John
Customer address Wisdom Apartments, Ludhiya
Customer id 2
Customer name Poul
Customer address New Empire road, Khandala
6.How do you define global variables?
Ans: Global variables begin with $. Uninitialized global variables have
the value nil and produce warnings with the -w option.Assignment to global variables
alters global status.It is not recommended to use global variables. They make programs cryptic.
Here is an example showing usage of global variable.
#!/usr/bin/ruby
$global_variable = 10
class Class1
def print_global
puts "Global variable in Class1 is #$global_variable"
end
end
class Class2
def print_global
puts "Global variable in Class2 is #$global_variable"
end
end
class1obj = Class1.new
class1obj.print_global
class2obj = Class2.new
class2obj.print_global
|
Here $global_variable is a global variable. This will produce following result:
NOTE: In Ruby you CAN access value of any variable or constant by putting a hash (#) character just before that variable or constant.
Global variable in Class1 is 10
Global variable in Class2 is 10
8.What is a Range?
Ans: