Home > Back-end >  What exactly is this line doing?
What exactly is this line doing?

Time:11-28

I don’t know Ruby at all just wondering what this line specifically is doing

bitrate = params[:bitrate] =~ /\A\d k\z/ ? params[:bitrate] : '3000k'

It appears to be validating the input but I’m not sure about the last colon

Just wondering what the code does specifically in plain English

CodePudding user response:

You're seeing the ternary operator in use. It has the form:

expr ? expr-when-true : expr-when-false

The first condition is evaluated in a boolean context, so anything that's treated as true will yield the first result. Otherwise, the second.

In Ruby you could also write as a one-liner:

bitrate = if params[:bitrate] =~ /\A\d k\z/ then params[:bitrate] else '3000k' end
  •  Tags:  
  • ruby
  • Related