Skip to content

Instantly share code, notes, and snippets.

@JoseJRVazquez
Created April 13, 2014 19:38
Show Gist options
  • Save JoseJRVazquez/10599057 to your computer and use it in GitHub Desktop.
Save JoseJRVazquez/10599057 to your computer and use it in GitHub Desktop.
so....
looks like that while its all fine and dandy for someone to have a getter and setter attribute for a class, so you can enter data and spit it back out, the question becomes what if you want a vlue set by default...for whatever bs reason.
The exmaple given is as follows:
class President #defines class
def initialize #defines method
p "Hello, I'm an instance of the President class!" #the action that is done or info entered by default
end #close the method
end #close the class
President.new #command to make a new one of these
#=> "Hello, I'm an instance of the President class!" #the output from the method
So i guess where this would work well is if someone on a website...
1) Signed in
2) Signed up
3) Didnt put in all their info into a form for unnecesary fields
Another example is as follows
class President #set class
attr_accessor :age #set/get attribute
def initialize #initialize command
@age = 55 #sets the default value when a new instance arrives
end #close method
end #close class
I tried this and found some cool shit
pres = President.new
p pres.age
#=> 55
I was able to play around with it and change the value of age easily when I i was in IRB.
But now it says this is a ridgid way of doing business. So they have a way to make it more flexible
class President #sets class
attr_accessor :age #getter/setter
def initialize(years) #initialize command for instance with "Years" as the varible
@age = years #sets years for age
end #end method
end #end class
pres = President.new(49) #sets a new president, but with a notation that the age is 49
p pres.age #asks what the presidents age is
#=> 49 #output
It allows the age to be set, only when a new instance is called up. So lets say a record is blank. But when a new record is made, it can set all of the features of the profile. So if it was a signup page, the new record can begin, with the new values filled in all at once rather than be ridgid.
Ok, I can see that.
So now it wants to do a twofer
class President #set the class
attr_accessor :age, :party #set the set/get for attributes age, and party
def initialize(years, party) #set method to set on initializing new instance
@age, @party = years, party #values to be set when initialized
end #end method
end #end class
pres = President.new(49, "Independent") #new instance, with age and party filled in
p "The President is #{pres.age} years old and is a member of the #{pres.party} party." #a statement utiizing those variables in a print output
#=> "The President is 49 years old and is a member of the Independent party." #The output
I tried to do this on my own as follows:
class Signup
attr_accessor :name, :uname, :membership
def initialize(name, uname, membership)
@name, @uname, @membership = name, uname, membership
end
end
client = Signup.new("John", "JohnDoe", "free")
client = Signup.new("Jose", "JoseJRVazquez", "paid")
p "Welcome #{client.name}, your user name is #{client.name} and your membership is #{client.membership}"
#This worked but only after I had to tweak it several times. Code is code.So i found the errors on my own and it worked out fine.
So now we are going to get fucked with, and they are introducing Class Methods
The within class methods I have written in up to this point are instance methods. That is, they are called on a unique instance of a class. There's another type of method that can be written in a class. Class methods pertain to the class itself, rather than a unique instance of the class. So they modified the President class to demonstrate this. They made the class slightly more specific and rename it UnitedStatesPresident:
Class UnitedStatesPresident #sets class
attr_accessor :age, :party #sets/gets
def initialize(years, party) #sets method
@age, @party = years, party #instance variables
end #closes method
def self.citizenship #sets class method
"United States of America" #sets class method action
end #ends class method
end #ends class
Trying this now in IRB
2.0.0-p451 :148 > class UnitedStatesPresident
2.0.0-p451 :149?> attr_accessor :age, :party
2.0.0-p451 :150?>
2.0.0-p451 :151 > def initialize(years, party)
2.0.0-p451 :152?> @age, @party = years, party
2.0.0-p451 :153?> end
2.0.0-p451 :154?>
2.0.0-p451 :155 > def self.citizenship
2.0.0-p451 :156?> "United States of America"
2.0.0-p451 :157?> end
2.0.0-p451 :158?> end
=> nil
2.0.0-p451 :159 > p UnitedStatesPresident.citizenship
"United States of America"
=> "United States of America"
EUREKA it works on its own: lets try a variant
This was awesome
#Tried this with a Class Metho
class Signup
attr_accessor :name, :uname
def initialize(name, uname)
@name, @uname = name, uname
end
def self.user
"Free Account"
end
end
client = Signup.new("John", "JohnDoe")
p "Hello #{client.name}, you have a #{Signup.user}, your handle is #{client.uname}"
But I have to remember that the class method has to be called with its own name not the name of the varible name action
os in this, which I may have set the record to client, I had to use the Classname.user to make it work in IRB
Aced the tests: but here ius the forumla I put together
class Car
attr_accessor :make, :model, :year
def initialize(make, model, year)
@make, @model, @year = make, model, year
end
def self.wheels
4
end
def self.axles
2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment