New Features & Tips Flashcards

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

Is this code valid?

def: increment(x) = x + 1

A

yes.

Ruby 3 new feature. Enless method

p increment(42) #=> 43

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
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
3
Q

Is that possible to measure the processing time in IRB?

A

yes. add this measure command in IRB

New feature of Ruby 3

irb(main):001:0> 3
=> 3
irb(main):002:0> measure
TIME is added.
=> nil
irb(main):003:0> 3
processing time: 0.000058s
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Is it possible to color the Ruby IRB text?

A
class String
  def red
    "\e[31m#{self}\e[0m"
  end

def green
“\e[32m#{self}\e[0m”
end
end

puts “Red again!”.red
puts “Green again!”.green
puts “Mixed %s with %s!” % [“red”.red, “green”.green]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
What is the alternate why to execute this code?
def play
    while !exceeded_max_guesses?
    	....
    end
end
A

Instead of a while not, we can simply use until:

until exceeded_max_guesses?
….
end

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