Class के Basics
एक class एक blueprint है जो describe करता है एक तरह की चीज़ें क्या जानती हैं और क्या कर सकती हैं, एक toy के लिए एक plan की तरह। आप इसका इस्तेमाल कई toys बनाने के लिए करते हैं।
In this page:
Syntax
class ClassName
# attributes and methods
end
object = ClassName.new
Class Basics
एक Ruby class class ClassName ... end से define होता है, data और behavior को एक single blueprint में bundle करते हुए जिससे .new से objects बनते हैं।
Class names conventionally CamelCase इस्तेमाल करते हैं, methods और variables के उलट जो snake_case इस्तेमाल करते हैं। Ruby में हर value, numbers और strings समेत, किसी class का एक object है।
Note:
Ruby class names को एक capital letter से शुरू होना ज़रूरी है -- यह language से enforced है, सिर्फ एक style convention नहीं।
उदाहरण: Class Basics
class Dog
def bark
puts "Woof!"
end
end
rex = Dog.new
rex.bark
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- एक class को lowercase में name करना, जैसे
class dog, जो एक syntax error है क्योंकि class names constants हैं। - पहले
Dog.newसे एक object बनाने की बजायDog.barkcall करना। - class या एक method के बाद
endभूल जाना।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: