Last active
April 8, 2018 18:58
-
-
Save mykiy/24d4d10d3b40895f567618a1a2e2ebf5 to your computer and use it in GitHub Desktop.
Test ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) Explain calling super and calling super() and give some example? | |
super and super() both inherits methods from parent class. super allows the arguments but super() dont allow the arguments. | |
Ex: | |
-- | |
class Niyati | |
def process(person,work) | |
"I am #{person} and am a #{work}" | |
end | |
end | |
class Ror < Niyati | |
def process() | |
super | |
end | |
end | |
=> | |
Ror.new.process("prasanth","ROR developer") | |
=> | |
I am prasanth and am a ROR developer | |
class Ruby < Niyati | |
def process(person,work) | |
super() | |
end | |
end | |
=> | |
Ruby.new.process("velu","ROR developer") | |
=> | |
Argument error given 0 expected 2 | |
--------------------------------- | |
2) when they should be used: ==, ===, eql?, equal? and give some example | |
These are equality operators and methods in ruby. It compares the strings length and content equal or not. | |
Methods only checks the values with same type. But, == and === are return true if values are the same not forces the same type. | |
Ex: | |
1 == 1.0, 1 === 1.0 => true | |
1.eql? 1.0 => false "12".length.equal? "ab".length =>true | |
3) tell diff for array.map(&:method_name) as a shorthand form of array.map { |element| element.method_name }. How exactly does it work? | |
In &:method_name, & is a unary operator which helps to convert method into proc_block. | |
Ex: | |
-- | |
arr = ['a','b','c'] | |
arr.map(&:upcase) | |
=> | |
['A','B','C'] | |
4) Find the below code | |
class A | |
def self.a(b) | |
if b > 0 | |
b * b | |
end | |
end | |
end | |
what will be output of | |
var1 = A.a(0) | |
var2 = A.a(2) | |
output: | |
--- | |
Calling self method using A.a(0), which returns nil because the condition is false. | |
=> A.a(0) = nil | |
=> A.a(2) = 4 | |
5) Can you call a private method outside a Ruby class using its object, give example? | |
Cant call the private method directly outside ruby class using object,it returns NoMethodError. | |
But we can use send method to access all the instance methods of current object. | |
Ex: | |
--- | |
class Klass | |
def pub | |
"I'm Public" | |
end | |
private | |
def pri | |
"I'm private" | |
end | |
end | |
Output: | |
----- | |
Klass.new.pri | |
=> NoMethodError private method pri | |
obj = Klass.new | |
obj.send(:pri) | |
=> | |
I'm private | |
end | |
6) Difference between clone and du | |
Clone and dup both used to create a shallow copy of object. But clone differ in 2 things. | |
1) freeze 2) maintain the singleton methods | |
Ex: | |
--- | |
[freeze] | |
a = [12,34,55,66,77] | |
a.freeze | |
b = a.clone | |
c = a.dup | |
b << 6 | |
=> | |
Cant modify frozen array. | |
c << 6 | |
=> 12,34,55,66,77,6] | |
[singledon methods] | |
obj = Object.new | |
def obj.iam | |
"I'm velu" | |
end | |
b = obj.clone | |
b.iam | |
=> | |
"velu" | |
c = obj.dup | |
c.iam | |
=> | |
NoMethodError | |
7) how can achieve method overloading in ruby give some example? | |
To achieve method overloading, we should use two different versions of method with same name. But ruby class can have only one method with same name. | |
By achieve this we can pass many arguments with the single method. | |
Ex: | |
--- | |
def some(*p) | |
if p.size < 2 | |
"less than" | |
else | |
"many arguments" | |
end | |
end | |
8) diff b/w if vs unless vs untill give some example? | |
Unless and until both are false block statement. When condition is false its block code is executed, when true it terminated. | |
Ex: | |
-- | |
i = 5 | |
until i >= 10 | |
p i | |
i += 1 | |
end | |
9) super keyword | |
Super method calls the parent method of the same name, and also with the same arguments. | |
10) three levels of method access control give some example ? | |
=> Public - Can access by everyone | |
=> private - Can access by implicit receiver only | |
=> protected - Can access by explicit reciever also | |
class Klass | |
def m | |
m1 | |
end | |
private | |
def m1 | |
"#{self.class}" | |
end | |
protected | |
def m2 | |
"#{self.class}" | |
end | |
end | |
-- | |
class Hlass < Klass | |
def m | |
m1 | |
end | |
end | |
--- | |
11) module vs class | |
In module is a collection of instance methods that included or extended to class. Mixin is best example for module. | |
We can create the instantiated object in class and also we can inherit the class to another class. | |
module Ror | |
def process | |
"learning" | |
end | |
end | |
class Niyati | |
include Ror | |
end | |
Niyati.new.Ror | |
=> learning | |
12) proc vs lambda | |
------ | |
=> lambda is a proc object | |
=> lambda expects correct arguments to pass in method, otherwise it returns argument error | |
15) Find duplication element in array | |
a = ["A", "B", "B", "C", "A", "a", "c"] | |
b = a.group_by { |a| a } | |
b.keep_if { |k,v| v.length > 1 } | |
=> | |
[A,A],[B,B} | |
a.uniq | |
=> which eliminates the duplicate values. | |
16) reverse a given string | |
h1 = "my name is abc" | |
h2 = h1.reverse | |
h2.split(' ' ).map(&:reverse).join(' ' ) | |
17) Array mixing with string, hash and integer number need to arranging order | |
a = [3, 1, "C", {:name=>"hari"}, "A"] | |
a.sort_by { |a| a.to_s } | |
=> | |
[1,3,"A","C", {:name => "hari" } | |
18) find the most repeat value and count | |
my_val = [1,1,1, 2,2,2, 5,5, 2,2,2] | |
freq = my_val.uniq.max_by { |i| my_val.count(i) } | |
p freq | |
=> | |
2 | |
19) write a regular expression Input value should allow 5 integer 1 character should not allow any special character | |
=> /(\d{5})+(\D{1})/ | |
20) write a regular expression for email | |
/\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment