Ruby Quiz Jan 2022 Flashcards
What does %x!ls! represents?
Same as back tick command output ls
What is the output?
>55.to_s(2)
“110111” - converts to binary
What is the output?
- nonzero?
- nonzero?
nil
1
The nonzero?() is an inbuilt method in Ruby returns self if the number is non-zero, else it returns nil.
def introduce1
__method__
end
def introduce2
__callee__
end
> introduce2
outout?
:introduce2
colors = [“cyan”, “magenta”, “yellow”, “white”];
What is
>Hash[*colors]
will give?
{“cyan”=>”magenta”, “yellow”=>”white”}
> a = [1,2,3,4]
_.to_s
_.class
what is the output?
“[1,2,3,4]”
String
_ repeats the last entered code in IRB
What is the meaning of this code?
> %I[ Ruby is Cool]
[:Ruby, :is, :cool]
Interpolated Array of symbols, separated by whitespace
Which one is correct? %w(ruby is ok) %i[ruby is ok] %q{ruby is ok} %r(ruby)
All are correct.these are non-alpha-numeric character delimiters:
%w(ruby is ok) => ["ruby", "is", "ok"] 3.0.0 :003 > %i[ruby is ok] => [:ruby, :is, :ok] 3.0.0 :004 > %q{ruby is ok} => "ruby is ok" 3.0.0 :005 > %r => /ruby is ok/ 3.0.0 :006 >
range = 1..Float::INFINITY
what would be the output?
=> 1..Infinity
range = 1..Float::INFINITY
range.lazy.map { |x| x+x }.first(10)
=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
array = %w(this is an array)
array * ‘ , ‘
What would be the output
=> “this, is, an, array”
join by
How to get range values apart from entries method?
>p *(1..5) 1 2 3 4 5 => [1, 2, 3, 4, 5]
my_proc = -> argument { puts argument } Which one is valid? a)my_proc.call('hello') b)my_proc.('hello') c)my_proc['hello']
all are valid
What would be the output?
> [*(‘a’..’e’)]
=> [“a”, “b”, “c”, “d”, “e”]
Which delimiter is used to execute shell commands in ruby?
%x[ ] # Interpolated shell command