29.4.2021 Ruby Quiz Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Can case statement be assigned to any variable?

A

yes. This value can be assigned to a variable preceding the case statement.

happy = case

when …

end

puts happy

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between Date and DateTime classes?

A

The Date class has no concept of minutes, seconds or hours. This class stores everything internally in terms of days.

The DateTime class is a subclass of Date and it can store seconds in addition to dates.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

> 3.days.ago

Is this valid method in Ruby? If not,

How we can get this output?

A

These methods are not available in pure Ruby,
they are added by the ActiveSupport component of Rails.

  1. day # ActiveSupport::Duration
  2. days.ago # ActiveSupport::TimeWithZone
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Consider the error. How to fix it?

irb(main):001:0> Date.today
Traceback (most recent call last):
2: from /Library/Ruby/Gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `'
1: from (irb):1
NameError (uninitialized constant Date)
Did you mean? Data
A

To use the Date class you need to

> require ‘date’.

You can get the current date using Date.today.

irb>Date.today

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to get all the month names and Week days names from Date class?

A

The Date class has some constants.

For example, there is an array with the months of the year and another with the days of the week.

require ‘date’

puts Date::MONTHNAMES #all months
puts Date::DAYNAMES #all days

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the ways to read a file using File class?

A

There are three different methods to read a file.

To return a single line, following syntax is used.

Syntax: f.gets
code…
To return the whole file after the current position, following syntax is used.

Syntax: f.read
code…
To return file as an array of lines, following syntax is used.

Syntax: f.readlines
[code…]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the Socket library subclasses from IO?

A

TCPSocket , UDPSocket ,UNIX Socket

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the following open mode implies?

w+
r+
a+

A

“w+” Read-write, truncates existing file to zero length
or creates a new file for reading and writing.

“r+” Read-write, starts at beginning of file.

“a+” Read-write, each write call appends data at end of file.
Creates a new file for reading and writing if file does
not exist.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between calling super and calling super()?

A

super - sends all arguments

super() - no arguments

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Difference between prepend and extend keywords?

A
prepend keyword need object creation from class when
being called.

Extend keyword don’t need object when being called

module Commentable
def comment
puts 'I love comments!'
end
end
class Post
extend Commentable
def comment
puts'I comment!'
end
end
Post.comment # I love comments!
#if its prepend keyword you need to do

Post.new.comment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Is this code valid?

def: increment(x) = x + 1

A

yes.

Ruby 3 new feature. Endless method

p increment(42) #=> 43

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

user = { name: ‘Oliver’, age: 29, role: ‘CTO’ }

in this code how you will print name and age pair alone?

A

user.except(:role)

Ruby 3 feature : except method

earlier we need to use active support in rails to do that

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the use of backtrace method in Exception?

A

This method returns any backtrace associated with the exception. The backtrace is an array of strings, each containing either “filename:lineNo: in `method”‘ or “filename:lineNo.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the following code return?

browsers = {
  :favorite => :chrome,
  :favorite => :firefox,
  :worst => :internet_explorer
}
browsers[:favorite]
A

:firefox
Duplicate keys are not allowed in a hash. When Ruby detects a duplicate key, it simply deletes the first key / value pair that is a duplicate.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Is the following hash valid?

weird_hash = {
[1, 2, 3] => Object.new(),
Hash.new => :mousie
}

A

The weird_hash is odd, but valid. Any object can be a key or value in a Ruby hash.

Symbols are typically used as hash keys because they are descriptive and efficient, but any object can be used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the following code print? Explain.

class Dog; end
p Dog.ancestors
A

[Dog, Object, Kernel, BasicObject]

The Dog class inherits from the Object class and the Object class inherits from the BasicObject class.

The Kernel module is mixed-in to the Object class. All user defined classes inherit from the Object class by default.

BasicObject is a very simplistic class and is not commonly used.

The Kernel module defines methods that are commonly used in Ruby like raise() and puts().

17
Q

What does the following code print? Explain.

module AAA
  def hi; 'AAA#hi'; end
end
class Money
  class << self
    include AAA
  end
end

p Money.hi

A
"AAA#hi"
The 'class << self' syntax is used to change the scope to Money's singleton class. The module's methods are added to the singleton class as instance methods when the module is included in the singleton class.

Money.singleton_methods # => [:hi]

18
Q

What does the following code print? Explain.

module M
  def hi
    'hey hey'
  end
end

class A
include M
end

p A.instance_methods.include? :hi

A

true

Methods defined in a module are accessible as instance methods when the module is included in a class.

19
Q

What does the following code print? Explain.

p self
p "***"
class Example
  p self
end
A

main
“***”
Example

A program starts in the top level scope. When the program encounters the class keyword, the scope changes to the class. When the context changes (the terms context and scope are synonymous), the self keyword is reassigned to a different object.

20
Q

What does the following code print? Explain.

bieber = 'person'
class Weird
  y = 'boo'
  def hi
    z = 'sleepy generation'
  end
end
fever = 'sickness'

p local_variables

A

[:bieber, :fever]

The program starts in the top level scope where the bieber local variable is defined. When the program encounters the class keyword, it exits the top level scope and does not return to the top level scope until the class definition is ended. The fever local variable is defined in the top level scope when it is reopened.