AnyCompiler

Ruby ka Introduction

Ruby ek dynamically typed, interpreted, fully object-oriented scripting language hai. Is sentence mein "fully" word 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 saath file kiya jaata hai — teeno hi dynamically typed scripting languages hain jinmein koi compile step nahi hota — lekin teen features Ruby ko genuinely 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 jiska 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 se alag hai). Ye course teeno ko first-class topics ki tarah treat karta hai, footnotes ki tarah nahi.

Ek jaldi preview

Yeh ek real Ruby program hai, is page ke apne compiler par actually execute kiya gaya — koi mockup nahi. Ye Ruby ke teen defining ideas ko ek saath 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?
Output
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 directly is method call ko handed kiya gaya hai. :ruby.class Symbol return karta hai, ek type jiska Python, JavaScript, Rust, Go, ya is site ki kisi bhi C-family language mein real counterpart nahi hai. Aur nil.nil? kaam karta hai kyunki 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. Next: your first program — puts, print, aur top to bottom run hona bina kisi compile step ke.

Practice exercise

5 ki class print karo, phir :ruby ki class, phir nil ki class, ek line mein ek.