The words "fruit", "build", "guide", and "ruin" sound very differently.
In most cases the phonogram "ui" is a long [oo], but not in the words "build", "guide", and "ruin". Why?
English Language Learners Stack Exchange is a question and answer site for speakers of other languages learning English. It only takes a minute to sign up.Sign up to join this community
There are various analyses of this.
The simplest issue to deal with is "ruin". Quite simply, "ui" doesn't act as a phonogram/digraph in this word. It's simply "u" followed by a separate "i". This sort of thing is common in English: consider that "create" doesn't have the "ea" sound of "leaf" or of "deaf", and "archaic" doesn't have the "ai" sound of "rain". In general, any sequence of two vowel letters in English can be interpreted as representing distinct vowels rather than as a digraph; it's just an ambiguous part of the spelling system. (Apparently, some people actually do pronounce "ruin" as "rune", although I've never heard this and I wouldn't recommend using it if you're a non-native speaker.)
"Guide" has the "gu" phonogram, which can represent /g/. It's most often seen before "e" (as in "guest") or "i" (as in "guild") but it also may occur before "a", as in "guard".
"Build" is the most difficult word to analyze. Its spelling is quite irregular, so there are not many similar cases. The spelling of this word is discussed in A Survey of English Spelling, by Edward Carney, and Dictionary of the British English Spelling System, by Greg Brooks; both of these sources say that rather than "ui" acting as a phonogram, this word has a "bu" phonogram that corresponds to /b/. The other examples provided to support this analysis are "buy" and "buoy" (note: "buoy" is only an example if it is pronounced as "boy"; it can also be pronounced in two syllables as "boo-y").
I won't go into the historical origins of these pronunciations as they're largely independent of the spelling patterns (actually, even the words spelled with "ui" pronounced /uː/ do not seem to all come from the same source historically).
Also, something to be aware of if you're learning British English is that technically, the phonogram "ui" corresponds to "ew/eu" or "long u", not "oo". There is no contrast between these vowels in standard English accents after "j", "r", or a consonant + "l", but you can see the difference in "nuisance", which in standard British English starts with /njuː/ rather than /nuː/.
As I said in a comment, the answer to "why" questions about language is usually "Because that's how it is".
But it is sometimes possible to give a bit of information about how it got that way.
Most instances ('suit', 'fruit') were borrowed from French, and both the spelling and the (original) pronunciation came from French (though the pronunciation has changed since).
"Ruin" was borrowed partly from French and partly from Latin, according to the OED; that may be why its pronunciation is different; or it may be because of the following 'n'.
"Build" is an oddity, that the OED cannot account for. It says: "The normal modern spelling of the word would be bild (as it is actually pronounced); the origin of the spelling bui- (buy- in Caxton), and its retention to modern times, are difficult of explanation."