AnyCompiler

Sab Kuch Object Hai

Python, JavaScript, Go, Rust, aur is site ki poori C-family mein, "primitive values" (numbers, booleans) aur "real objects" jin par aap methods call karte ho, ke beech ek hard line hoti hai. Ruby mein aisi koi line nahi hai. 5 Integer ka instance hai. true TrueClass ka instance hai. nil NilClass ka instance hai. Sab Object se inherit karte hain, aur sab method calls ka response exactly waise hi dete hain jaise ek string ya custom class instance deta.

puts 5.times.to_a.inspect
puts 5.class
puts nil.nil?
puts nil.class
puts "hello".class
puts 42.respond_to?(:+)
Output
[0, 1, 2, 3, 4]
Integer
true
NilClass
String
true

5.times bina block ke ek Enumerator return karta hai — khud ek object — jise .to_a phir ek concrete array mein convert kar deta hai. respond_to?(:+) 42 (ek Integer object) se poochta hai ki wo + naam ka message samajhta hai ya nahi, aur wo samajhta hai, kyunki + Integer par ek ordinary method ki tarah implement hota hai, special compiler syntax nahi. Ye wo foundation hai jis par baaki ka course build hota hai: methods, blocks, aur mixins sab sense banate hain jab aap accept kar lete ho ki Ruby mein koi value aisi nahi hai jo method-call system se bahar baithi ho. Next: control flow — if/elsif/unless expressions ki tarah with a real value, plus Ruby ka khud ka undefined-method error.

Practice exercise

3.times ko .to_a se array mein convert karo aur .inspect se print karo, phir nil.nil? print karo.