Forum: Ruby help me understand (method 1 : 0)

Posted by Soichi Ishida (soichi)
on 2012-11-14 06:07
I got this piece of code from "RSpec Book" page 85


    def exact_match_count(guess)
      (0..3).inject(0) do | count, index |
        count + (exact_match?(guess, index) ? 1 : 0)
      end
    end

But I don't quite understand

   ...) ? 1 : 0)

part.

Could anyone help me understand this?

soichi
Posted by Kiswono Prayogo (Guest)
on 2012-11-14 06:12
(Received via mailing list)
is the ternary if operator..

just try these lines on irb:

print ( true ? '1. this part returned' : '2. this part not returned' )
print ( false ? '1. this part not returned' : '2. this part returned' )
Posted by Soichi Ishida (soichi)
on 2012-11-14 06:19
> just try these lines on irb:
>
> print ( true ? '1. this part returned' : '2. this part not returned' )
> print ( false ? '1. this part not returned' : '2. this part returned' )

Thanks I see it now!

soichi
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.