29.4.2021 Ruby Quiz Flashcards
Can case statement be assigned to any variable?
yes. This value can be assigned to a variable preceding the case statement.
happy = case
when …
end
puts happy
What is the difference between Date and DateTime classes?
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.
> 3.days.ago
Is this valid method in Ruby? If not,
How we can get this output?
These methods are not available in pure Ruby,
they are added by the ActiveSupport component of Rails.
- day # ActiveSupport::Duration
- days.ago # ActiveSupport::TimeWithZone
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
To use the Date class you need to
> require ‘date’.
You can get the current date using Date.today.
irb>Date.today
How to get all the month names and Week days names from Date class?
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
What are the ways to read a file using File class?
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…]
What are the Socket library subclasses from IO?
TCPSocket , UDPSocket ,UNIX Socket
What are the following open mode implies?
w+
r+
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.
What is the difference between calling super and calling super()?
super - sends all arguments
super() - no arguments
Difference between prepend and extend keywords?
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
Is this code valid?
def: increment(x) = x + 1
yes.
Ruby 3 new feature. Endless method
p increment(42) #=> 43
user = { name: ‘Oliver’, age: 29, role: ‘CTO’ }
in this code how you will print name and age pair alone?
user.except(:role)
Ruby 3 feature : except method
earlier we need to use active support in rails to do that
What is the use of backtrace method in Exception?
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.”
What does the following code return?
browsers = { :favorite => :chrome, :favorite => :firefox, :worst => :internet_explorer } browsers[:favorite]
: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.
Is the following hash valid?
weird_hash = {
[1, 2, 3] => Object.new(),
Hash.new => :mousie
}
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.