← Back to Ruby Course | Chapter 9: OOP | Lesson 1 of 7

Class के Basics

एक class एक blueprint है जो describe करता है एक तरह की चीज़ें क्या जानती हैं और क्या कर सकती हैं, एक toy के लिए एक plan की तरह। आप इसका इस्तेमाल कई toys बनाने के लिए करते हैं।

In this page:

  1. Class Basics
Syntax
ruby
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

markup
class Dog
  def bark
    puts "Woof!"
  end
end

rex = Dog.new
rex.bark
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. #}
आम गलतियां
  1. एक class को lowercase में name करना, जैसे class dog, जो एक syntax error है क्योंकि class names constants हैं।
  2. पहले Dog.new से एक object बनाने की बजाय Dog.bark call करना।
  3. class या एक method के बाद end भूल जाना।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.