ROR Interview Questions Experienced
- Difference between the map & each?
- The map will return the new array with modified values.
- Each will return the same array with an object.
- Write the program to sort the array in ruby without using the sort method?
- def ary_sort(list, new_array = nil)return new_array if list.size <= 0if new_array == nilmin = list.minnew_array = [] endary_sort(list, new_array)new_array << min list.delete(min)end
- ary_sort([1,6,5,3,8,24,18])
- Write the Program for array pass the arguments?
- Input will be a = [2,3,4] ,a.add_no(2) ====> Output - [4,5,6]
- Array.class_eval dodef add_no(n)self.map{|x| x+ n}endend
- Difference between the find and find_by in rails?
- find_by() returns the nil if the record does not exist in the database
- find() returns the ActiveRecord::RecordNotFound exception f the record does not exist in the database.
- How do access the private method outside the class?
- class Koti
- private
- def test_priv
- puts "this is the private method"
- end
- end
- Koti.new.instance_eval(:test_priv)
- Given two arrays of integers nums and index. Your task is to create a target array under the following rules:
- * Initially target array is empty.
- * From left to right read nums[i] and index[i], insert at index index[i] the value nums[i] in target array.
- * Repeat the previous step until there are no elements to read in nums and index.
- Return the target array.
- Example 1:
- Input: nums = [0,1,2,3,4], index = [0,1,2,2,1]
- Output: [0,4,1,3,2]
- Explanation:
- nums index target
- 0 0 [0]
- 1 1 [0,1]
- 2 2 [0,1,2]
- 3 2 [0,1,3,2]
- 4 1 [0,4,1,3,2]
- Example 2:
- Input: nums = [1,2,3,4,0], index = [0,1,2,3,0]
- Output: [0,1,2,3,4]
- Explanation:
- nums index target
- 1 0 [1]
- 2 1 [1,2]
- 3 2 [1,2,3]
- 4 3 [1,2,3,4]
- 0 0 [0,1,2,3,4]
- What is the garbage collection?
- Garbage collection allows the removal of the pointer values that is left behind when the execution of the program ends. -It frees the programmer from tracking the object that is being created dynamically on runtime
- The Ruby garbage collector also allocates memory. It’s a complete memory management system.
- What is the difference between the attr_accessor,attr_accesible?
- attr_accessor -> attr_accessor is a Ruby method that makes a getter and a setter.
- attr_accesible -> attr_accesible is Rails method that allows you to pass in values to a mass assignment: new(attrs) or update_attributes(attrs)
attr_accessor
is ruby code and is used when you do not have a column in your database, but still want to show a field in your forms. The only way to allow this is toattr_accessor :fieldname
we should use the Strong parameter instead ofattr_accessor
isgetter
,setter
method. whereasattr_accessible
is to say that particular attribute is accessible or not.attr_accessible
to protect from mass assignment.- attr_accessor example
class Address attr_accessor :city def initialize @city = "" end end
Address.new.city = "Miryalaguda"
class Person def name @name end def name=(str) @name = str end end person = Person.new person.name = 'kotesh' person.name # => "kotesh"
class Person attr_accessor :name end
person = Person.new person.name = "koti" person.name # => "koti"
- Write the program for the below input
- Write the program for the below input
- Input data is ===> array = [1, 2] snap = Snapshot.new(array) array[0] = 3; array = snap.restore() puts array print(array.join()) #It should log "1,2" array.push(4) array = snap.restore() print(array.join()) #It should log "1,2"
- Program is
- class Snapshot def initialize(inp) @start_val =[] inp.each{|x| @start_val << x} @start_val.freeze end def restore() new_strt_val = [] @start_val.each{|y| new_strt_val<< y } return new_strt_val end end
- What are the design patterns in ruby?
- Service Objects
View Objects
Query Objects
Decorators
Form Objects
Value Objects
Policy Objects - Write the program for palindrome in ruby?
- a = "RKR"
new_ar = []
a.each_with_index do |i,index|
new_ar << a[(a.length-1) - index]
end
if a == new_ar.join(",")
puts "palindrome number"
else
puts "It's not palindrome number"
end
- a = "RKR"
- How to pass the block in the method using ruby?
- def method1 puts yield end method1{"testing block with arguments"}
- Difference between the proc & block?
- Blocks are used extensively in Ruby for passing bits of code to functions
- Procs behave like blocks, but they can be stored in a variable.
- A block is a collection of code enclosed in a do/end statement or between braces
def block_method
puts "we are in the method"
endblock_method { puts "The block is called"} O/p => we are in the method- Given array how many times "1" is repeated?
- /p => a = [1,0,1,1,1,1,0,1] O/p => 6 Solution: a.collect{|x|x if x == 1}.inject(:+) a.count(1) a.inject(:+).
- What is the use of webpackers in rails?
- Webpacker is a gem that allows easy integration of JavaScript pre-processor and bundler with Rails. It provides various helpers and configuration options to use webpack easily with Rails
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
Comments
Post a Comment