Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 23, 2022 07:39 pm GMT

Ruby quiz

Hi, dev.to!
Here is my first post there. I hope there are people who like mind games. So I would like to introduce you to some interesting puzzles.

1. Choose the correct result of the code (ruby 3.0):

{ language: 'ruby, 'position' => 'engineer' }.transform_keys({ language: 'rust }, &:to_sym)
  • {"rust"=>"ruby", :position=>"engineer"}
  • { :rust => 'ruby', :position => 'engineer' }
  • { "rust" => :ruby, 'position' => :engineer }
  • { :rust => :ruby, :position => :engineer }

Answer {"rust"=>"ruby", :position=>"engineer"}

2. Choose the correct result of the code (ruby 3.0):

{ e: :n, g: :i, n: :e, e: :r }.except(:e)
  • NoMethodError (undefined method 'except')
  • {:g=>:i, :n=>:e}
  • {:g=>:i, :n=>:e, :e => :r}
  • {:e => :n, :g=>:i, :n=>:e}

Answer
{:g=>:i, :n=>:e}

3. Choose the wrong way to call a lambda

  • ->(){}::call
  • ->(){}[]
  • ->(){}()
  • ->(){}::===

Answer
->(){}()

4. Choose the correct result of the code

!?q::!. |001
  • true
  • false
  • raise an error
  • 1

Answer
false

5. Choose the correct way to create an array [0,1,2,3,4,5]

  • Array[0..5]
  • (0..4).take(5)
  • [*0..5]
  • String(012345).split('').map(&:to_i)

Answer
[*0..5]

6. There is a code

class Animal  @@count = 0  def self.inc    @@count += 1  end  def self.count    @@count  end  endclass Cat < Animal  @@count = 100  def self.count    @@count  endendAnimal.incCat.inc

Choose the correct result of the code

[Animal.count, Cat.count]
  • [1, 101]
  • [101, 101]
  • [1, 102]
  • [102, 102]

Answer
[102, 102]

7. There is a code

class Item  def self.count    $COUNT  end  def self.increment    $COUNT += 1  endendBEGIN { $COUNT = 0 }Item.increment

Choose the correct result of the code

Item.count
  • 0
  • 1
  • 101
  • undefined method '+' for nil:NilClass

Answer 1


Original Link: https://dev.to/neodelf/ruby-quiz-4hb4

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To