DEV Community

Cover image for Ruby quiz
Andrew Molchanov
Andrew Molchanov

Posted on

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)
Enter fullscreen mode Exit fullscreen mode
  • {"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)
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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  
end

class Cat < Animal
  @@count = 100

  def self.count
    @@count
  end
end

Animal.inc
Cat.inc
Enter fullscreen mode Exit fullscreen mode

Choose the correct result of the code

[Animal.count, Cat.count]
Enter fullscreen mode Exit fullscreen mode
  • [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
  end
end

BEGIN { $COUNT = 0 }
Item.increment
Enter fullscreen mode Exit fullscreen mode

Choose the correct result of the code

Item.count
Enter fullscreen mode Exit fullscreen mode
  • 0
  • 1
  • 101
  • undefined method '+' for nil:NilClass

Answer 1

Top comments (10)

Collapse
 
mxdavis profile image
Malki Davis

This was some really good stuff. I was not able to reproduce the first one in my console:

pry(main)> { language: 'ruby', 'position' => 'engineer' }.transform_keys({ language: 'rust' }, &:to_sym)
ArgumentError: wrong number of arguments (given 1, expected 0)

Thanks for some good fun!

Collapse
 
neodelf profile image
Andrew Molchanov

Hi!
What version of ruby did you use? I added a 3.0.0 version of ruby for which question in the question. Please, check it

Collapse
 
mxdavis profile image
Malki Davis

Thanks, got it!

Collapse
 
w3ndo profile image
Patrick Wendo

Well damn, I really do not know Ruby as much as I thought I did.

Collapse
 
neodelf profile image
Andrew Molchanov

I believe nobody will use this on production, it is just for fun πŸ€ͺ

Collapse
 
w3ndo profile image
Patrick Wendo

Regardless, I was looking into competitive programming and well, I may end up using this.

Thread Thread
 
neodelf profile image
Andrew Molchanov

It is warm sense that your post could help somebody
❀️

Collapse
 
gemathus profile image
Gerardo Mathus • Edited

Nice! Maybe in the future (if you do any more of these), it'd be nice to have the documentation reference link when applicable. That way we can take a deeper dive.
Cheers! πŸ‘πŸ‘πŸ‘

Collapse
 
neodelf profile image
Andrew Molchanov

Got it!

Collapse
 
neodelf profile image
Andrew Molchanov

Nice! I believe you mentioned the 4th puzzle)