Ruby Ka Taaruf
Ruby ek dynamically typed, interpreted, mukammal taur par object-oriented scripting language hai. Is sentence mein "fully" word waqai kaafi matter karta hai: Ruby mein, ek integer bhi object hota hai. 5.times { ... } number 5 par ek method call hai. nil.nil? khud nil par ek method call hai. Koi bhi "primitive" nahi hota jo object system se bahar baitha ho — jo bhi aap touch karte ho wo kisi class ka instance hota hai, aur har class runtime par inspection aur extension ke liye open hoti hai.
Ruby ko aksar Perl aur Python ke sath file kiya jata hai — teeno hi dynamically typed scripting languages hain jin mein koi compile step nahi hota — lekin teen features Ruby ko waqai apni cheez banate hain, dono mein se kisi ek ka reskin nahi: blocks (apna khud ka do...end/{ } syntax jo method call mein code pass karta hai, lambda literal se alag), symbols (:name, ek lightweight immutable identifier type jis ka Python, JS, Go, Rust, ya C-family mein koi equivalent nahi hai), aur mixins via module/include (ek multiple-inheritance jaisa composition model jo Java/C#/Kotlin ke single-inheritance-plus-interfaces ya Rust ke traits ke bar-aks hai). Ye course teeno ko first-class topics ki tarah treat karta hai, footnotes ki tarah nahi.
Ek Chhoti Si Jhalak
Ye hai ek real Ruby program, jo waqai is page ke apne compiler par execute hua hai — koi mockup nahi. Ye Ruby ke teen defining ideas ko ek sath touch karta hai: times ko diya gaya ek block, ek symbol, aur nil jo method call ka response de raha hai.
puts "Ruby in action:"
5.times { |i| print i, " " }
puts
puts :ruby.class
puts nil.nil?
Ruby in action: 0 1 2 3 4 Symbol true
5.times { |i| ... } koi loop keyword nahi hai — times Integer 5 par ek ordinary method hai, aur { |i| ... } ek block hai, code ka ek chunk jo seedha us method call ko handed kiya gaya hai. :ruby.class Symbol return karta hai, ek type jis ka Python, JavaScript, Rust, Go, ya is site ki kisi bhi C-family language mein real counterpart nahi hai. Aur nil.nil? kaam karta hai kyunke nil — Ruby ka absence-of-a-value — khud ek object hai (NilClass ka ek instance) jo methods ka response deta hai, kisi aur value ki tarah. Aage: your first program — puts, print, aur top se bottom run hona baghair kisi compile step ke.
Practice exercise
5 ki class print karo, phir :ruby ki class, phir nil ki class, ek line mein ek.