Skip to content

Commit

Permalink
Chapter2-Organizing Objects With Classes
Browse files Browse the repository at this point in the history
  • Loading branch information
rabajaj0509 committed Aug 3, 2017
1 parent 0ff2b71 commit 043bba2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Chapter2-Organizing Objects With Classes/initialize.rb
Original file line number Diff line number Diff line change
@@ -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}"
16 changes: 16 additions & 0 deletions Chapter2-Organizing Objects With Classes/instance_variables.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 043bba2

Please sign in to comment.