TOP Advanced Interview Questions in Ruby on Rails - ByteScout
  • Home
  • /
  • Blog
  • /
  • TOP Advanced Interview Questions in Ruby on Rails

TOP Advanced Interview Questions in Ruby on Rails

You are at the right place if you are searching for advanced interview questions in Ruby on Rails. There are a plethora of opportunities from a majority of reputed companies around the world. According to research on Ruby on Rails, it was found that it has a market share of about 4.3%.so you still have the possibility of moving ahead in your career in Ruby on Rails.

TOP Advanced Interview Questions in Ruby on Rails

Below are some of the most commonly asked interview questions about Ruby on Rails:

1. After the code below is executed will val1 and val2 be equal? Explain your answer.

val1 = true and false  
val2 = true && false

Answer:

These observations may appear to be similar, but they are not because of the number of operations. The “and” and “or” operators have lower priority than the = operator. On the other hand, the && and || operators have higher precedence than the = operator depending upon the operations’ order.

For clarifying this, below is the same code but parenthesis is applied for clarifying the existing order:

(val1 = true) and false # result is val1 being equal to true
val2 = (true && false) # result is val2 being equal to false

Unexpectedly it is a fabulous example of why using parenthesis for specifying your papa is usually a good practice in any language. But whether or not you use parenthesis, it becomes essential to become aware of these order of operational rules and ensure that you are adequately determined, meaning went to operate and vs. &&/||.

2. Is the line of code below a valid Ruby code? If so, what does it do? Explain your answer.

-> (a) {p a}["Hello world"]

Answer:

Yes, the above-mentioned Ruby code is valid. Here is what it does:

A new Proc is produced by the -> operator, which is one of Ruby’s functions.

One parameter is taken by this particular Proc (namely, a). Ruby executes the block p a when the proc is called as it is the equivalent of puts(a.inspect) what’s up tell but useful variation which is p is seldom better than puts for debugging. The string that is passed to it is simply printed by the proc.

The proc could be called using either the call method or using the square bracket syntax, so this line of code also invokes the proc and passes it to the string “Hello World.”

A proc that requires a single parameter is created by this line of code (a) in which it prints out and (b) revokes the proc, and then it is passed to the string “Hello world.” So, in short, this line of code print “Hello world.”

3. Differentiate between calling super and calling super()?

Answer:

A super call invokes the parent method with the same arguments passed to the child method invokes the parent method with the same arguments passed to the child method. Therefore an error will occur if the child’s views do not match what is being expected by the parent.

Whereas, in a call to super(), the parent method is invoked without any argument as probably expected. But as always, being precise in your code is a good thing.

4. You often see the trick of using an expression as an array.map(&:method_name) in a Ruby Code as a shorthand form of an array.map { |element| element.method_name }. How exactly does it work?

Answer:

When we pass a parameter with & in front of it that is to be used as a block and Ruby will call to_proc ornate in practice for making it usable as a block. Symbol#to_proc returns to a proc that will invoke the procedure of the corresponding name on whatever is passed to it enabling your little trick to work.

5. What is wrong with the following controller code? What would be the result of Leaving this code in a production app? How can it be fixed?

class MyController < ApplicationController
      def options
        options = {}
        available_option_keys = [:first_option, :second_option, :third_option]
        all_keys = params.keys.map(&:to_sym)
        set_option_keys = all_keys & available_option_keys
        set_option_keys.each do |key|
          options[key] = params[key]
        end
        options
      end
    end

Answer:

It is risky to convert user-supplied parameters to symbols as symbol objects in Ruby are not collected by garbage. A series of requests with random keys turned into characters that will quickly drain your server’s available memory by taking down your site could be sent by an attacker.

It could be fixed in two ways. The first day will be using slice for eliminating values from the params hash data not valid on option keys. It would look like this:

params.slice(*available_option_keys)

Some may argue that using string for your options would simply be better unless you have an extensively used number of option keys; you won’t save that much memory by using simple keys instead.

6. What is CSRF? How do Rails protect against it?

Answer:

The full form of CSRF is cross-site request forgery. It is a type of attack where the attacker offers a build on your account to a different website, possibly causing damage or disclosing sensitive information. As browsers will automatically e enter cookies for a domain on a request if you were freshly log in to the target site, the attacker’s demand will appear from you as a logged-in user.

For protecting yourself against CSRF attacks, protect_from_forgery could be added to your application controller. It will then cause Rails to require a CSRF token before accepting any POST, PUT, or DELETE requests. It is introduced as a hidden field in every form built using Rails by builders. It is also incorporated as a header in GET request so that other none form-based mechanisms for transmitting a post could use it. Attackers are prevented from seizing the CSRF token by the policy of the same-origin browser.

7. What is understood by ERB in Ruby on Rails?

Answer:

This web application framework offers a program known as ERB. It stands for Embedded Ruby. It could also be acknowledged when you put the codes compared to Ruby into HTML files quickly. Even when they are combined, users have nothing to worry about, which is one of the best things about this program.

   

About the Author

ByteScout Team ByteScout Team of Writers ByteScout has a team of professional writers proficient in different technical topics. We select the best writers to cover interesting and trending topics for our readers. We love developers and we hope our articles help you learn about programming and programmers.  
prev
next