Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Way of Nested If Statement

Tags:

ruby

Right now I have I feel like a very ugly way of setting a variable to a value depending on if it returns an empty string or not. Below is the method in question (it uses Nokogiri, but that doesn't really matter for this question).

def get_let(response)
    if response.css('A').empty?
        if response.css('B').empty?
            let = ''
        end
        let = response.css('B')
    else
        let = response.css('A')
    end

    return let
end
like image 798
yeenow123 Avatar asked Feb 06 '26 21:02

yeenow123


1 Answers

def get_let(response)
    let = response.css('A')
    let = response.css('B') if let.empty?
    let = '' if let.empty?
end
like image 144
sethcall Avatar answered Feb 09 '26 10:02

sethcall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!