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