forked from rabajaj0509/Ruby-concepts-step-by-step
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter2-Organizing Objects With Classes
- Loading branch information
1 parent
0ff2b71
commit 043bba2
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
Chapter2-Organizing Objects With Classes/instance_variables.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |