Ruby Quiz 25.2.2021 Flashcards
class ABC def xyz puts "xyz in ABC" end end. #for this code, which one is true? 1.ABC::new::xyz 2.ABC::new.xyz 3.ABC.new::xyz 4.ABC.new.xyz
- ABC::new::xyz
- ABC::new.xyz
- ABC.new::xyz
- ABC.new.xyz
all are true
output “xyz in ABC”
Is the below Ruby code valid?
-> (a) {p a}[“Hello world”]
If so, what does it do? Explain your answer.
The -> operator creates a new Proc, which is one of Ruby’s function types. (The -> is often called the “stabby proc”.)
This particular Proc takes one parameter (namely, a). When the Proc is called, Ruby executes the block
puts a
Which one is true?
my_lambda = -> { puts “Lambda called” }
a) my_lambda.call
b) my_lambda.()
c) my_lambda[]
d) my_lambda.===
All are true.
a) my_lambda.call
b) my_lambda.()
c) my_lambda[]
d) my_lambda.===
These methods are used to call the lambda
Which of the following is true?
defining a regular expression:
a) Regexp.new( ‘string pattern’ [, options ] )
b) /string pattern/
c) %r{string pattern}
d) ->(x){string pattern}
First three syntax are true.
a) Regexp.new( ‘string pattern’ [, options ] )
b) /string pattern/
c) %r{string pattern}
d) ->(x){string pattern}
fourth one is lambda syntax
What are the meaning of the following character ranges in Regular Expression?
\w
\d
\s
\w is equivalent to [0-9a-zA-Z_]
\d is the same as [0-9]
\s matches white space (tabs, regular space, newline)
What are the meaning of the following character ranges in Regular Expression?
\W
\D
\S
\W anything that’s not in [0-9a-zA-Z_]
\D anything that’s not a number
\S anything that’s not a space
What is the use of the method “named_capture” in Regexp?
It will return a hash representing information about named captures of rxp.
A key of the hash is a name of the named captures. A value of the hash is an array which is list of indexes of corresponding named captures.
/(?< foo >.)(?< bar >.)/.named_captures #=> {"foo"=>[1], "bar"=>[2]}
What is the use of “to_c” method in String?
It will return the complex form of the input sting.
‘9’.to_c #=> (9+0i)
‘2.5’.to_c #=> (2.5+0i)
a = “ 3”
How you will get this string?(without concatenation)
“Ruby 3”
a = “3”
a.prepend(“Ruby “)
puts a
What is the use of abs2 method in Complex class?
gives square of the result
Complex(5).abs2 =>25
What is the alternative way of expressing ‘if not’.?
unless # if !(aDay == 'Saturday' or aDay == 'Sunday')
unless aDay == 'Saturday' or aDay == 'Sunday' daytype = 'weekday' else daytype = 'weekend' end return daytype
How “loop” keywords can be used in Ruby?
loop do
end ============================= i=0 loop { puts(arr[i]) i+=1 if (i == arr.length) then break end }
What is the difference between #remove_method and #undef_method?
#undef_method( ) removes all methods, including the inherited ones.
#remove_method( ) removesall the methods, incase of inheritance, it leaves inherited methods.
Is there an equivalent of “continue” in Ruby?
The Ruby next statement is used to skip loop’s next iteration. Once the next statement is executed, no further iteration will be performed.
The next statement in Ruby is equivalent to continue statement in other languages.
How to display the inbetween values in a range?
print (1..7) #=> [1, 2, 3, 4, 5, 6, 7]
entries, to_a