From 043bba2202fa3bf2ec2c9bb39949c9aceaced73e Mon Sep 17 00:00:00 2001 From: Rahul Bajaj Date: Fri, 4 Aug 2017 03:27:32 +0530 Subject: [PATCH] Chapter2-Organizing Objects With Classes --- .../initialize.rb | 18 ++++++++++++++++++ .../instance_variables.rb | 16 ++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 Chapter2-Organizing Objects With Classes/initialize.rb create mode 100644 Chapter2-Organizing Objects With Classes/instance_variables.rb diff --git a/Chapter2-Organizing Objects With Classes/initialize.rb b/Chapter2-Organizing Objects With Classes/initialize.rb new file mode 100644 index 0000000..088d662 --- /dev/null +++ b/Chapter2-Organizing Objects With Classes/initialize.rb @@ -0,0 +1,18 @@ +#The initialize method is a method that will be executed +#every time you create a new instance of the class. + +class Ticket + attr_accessor :venue, :price + + def initialize(venue, price) + @venue = venue + @price = price + end +end + +th = Ticket.new("Town Hall", 1000) +cc = Ticket.new("Convention Center", 1500) + +puts "We have created two tickets." +puts "The first is for #{th.venue} and its price is #{th.price}" +puts "The first is for #{cc.venue} and its price is #{cc.price}" diff --git a/Chapter2-Organizing Objects With Classes/instance_variables.rb b/Chapter2-Organizing Objects With Classes/instance_variables.rb new file mode 100644 index 0000000..34c9005 --- /dev/null +++ b/Chapter2-Organizing Objects With Classes/instance_variables.rb @@ -0,0 +1,16 @@ +#An instance variable initialized in one method inside a class can be +#used by any method definition within that class. + +class Person + def set_name(string) + @name = string + end + + def get_name + p "The name of the person is : #{@name}" + end +end + +rahul = Person.new +rahul.set_name("rahul") +rahul.get_name