Ques 1. What are Class Variables? How to define them?
Class variables are created using the @@ prefix to denote the variable as class level.
It works just like any other variable, however in the case of inheritance it works more like a static variable that is accessed across all variable instances.
Another example can be found here:
class DemoClass
@@my_var = nil
def initialize
@@my_var = "hello world"
end
def my_var
puts @@my_var
end
end
class Demo2Class < DemoClass
def initialize
@@my_var = "goodbye world"
end
end
demo1 = DemoClass.new
demo1.my_var
demo2 = Demo2Class.new
demo2.my_var
demo1.my_var
The output would be as shown below:
hello world
goodbye world
goodbye world
Ques 2. How to define Instance variables?
Instance variables are defined using single @ symbol.
@foo = "Hello"
Within a class they can be declared as below:
class Animal
attr_accessor :name, :age
end
Next you can query an object instance to find which instance variables it has.
anim = Animal.new
anim.instance_variables
=> [ ]
anim.name="John"
anim.age = 3
=> [:@age, :@name]
In the above case we did not put the @ symbol before the instance variables but it is implied.
Ques 3. How can you dynamically define a method body in Ruby on Rails?
An instance method can be defined dynamically with
Module#define_method(name, body),
where name is the method’s name given as a Symbol, and body is its body given as a Proc, Method, UnboundMethod, or block literal. This allows methods to be defined at runtime, in contrast to def which requires the method name and body to appear literally in the source code.
class Conjure
def self.conjure(name, lamb)
define_method(name, lamb)
end
end
# Define a new instance method with a lambda as its body
Conjure.conjure(:glark, ->{ (3..5).to_a * 2 })
Conjure.new.glark #=> [3, 4, 5, 3, 4, 5]
Module#define_method is a private method so must be called from within the class the method is being defined on. Alternatively, it can be invoked inside class_eval like so:
Array.class_eval do
define_method(:second, ->{ self.[](1) })
end
[3, 4, 5].second #=> 4
Kernel#define_singleton_method is called with the same arguments as Module#define_method to define a singleton method on the receiver.
File.define_singleton_method(:match) do |file, pattern|
File.read(file).match(pattern)
end
File.match('/etc/passwd',/root/) #=> #<MatchData "root">
Ques 4. What is Range?
Range is a great way to declare continuous variables. You should use it to declare arrays and other types of collections.
range1 = (1..4).to_a
=> [1, 2, 3, 4]
puts range1
1
2
3
4
You can also create strings in this format and it fills in the interim values automatically.
range2 = ('bar'..'bat').to_a
puts range2
bar
bas
bat
Since the end result of using range is an array you can also iterate over it just like any other array.
range2.each do |str|
puts "In Loop #{str}"
end
This produces the result as shown below:
In Loop bar
In Loop bas
In Loop bat
Ques 5. How can you implement method overloading in Ruby on Rails?
This one’s a tricky question. If you have a background in Java then you must know that method overloading is simply multiple methods with same name but different signatures/parameters.
In the case of Ruby method overloading is not supported.
However, it does support the overall goal of passing variable number of parameters to the same method. You would implement it like this:
class MyClass
def initialize(*args)
if args.size < 2 || args.size > 3
puts 'This method takes either 2 or 3 arguments'
else
if args.size == 2
puts 'Found two arguments'
else
puts 'Found three arguments'
end
end
end
end
The output can be seen here:
MyClass.new([10, 23], 4, 10)
Found three arguments
MyClass.new([10, 23], [14, 13])
Found two arguments
SO: You can get the same effect as method overloading but you just have to manage the number of variables inside your method itself.