Quotes help make search much faster. Example: "Practice Makes Perfect"
Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts
Wednesday, January 23, 2013
Opening An Account
#Codecademy sample solution
class Account
attr_reader :name
attr_reader :balance
def initialize(name, balance=100)
@name = name
@balance = balance
end
private
def pin
@pin = 1234
end
def pin_error
return "Access denied: incorrect PIN."
end
public
def display_balance(pin_number)
if pin_number === pin
puts "Balance: $#{@balance}."
else
puts pin_error
end
end
def withdraw(pin_number, amount)
if pin_number === pin
@balance -= amount
puts "Withdrew #{amount}. New balance: $#{@balance}."
else
puts pin_error
end
end
end
#I wanna be a billionaire, so frekin' bad
checking_account = Account.new("Jim", 1_000_000_000)
Making a Withdrawal
#Codecademy sample solution
class Account
attr_reader :name
attr_reader :balance
def initialize(name, balance=100)
@name = name
@balance = balance
end
private
def pin
@pin = 1234
end
def pin_error
return "Access denied: incorrect PIN."
end
public
def display_balance(pin_number)
if pin_number === pin
puts "Balance: $#{@balance}."
else
puts pin_error
end
end
def withdraw(pin_number, amount)
if pin_number === pin
@balance -= amount
puts "Withdrew #{amount}. New balance: $#{@balance}."
else
puts pin_error
end
end
end
Displaying the Balance
#Codecademy sample solution
class Account
attr_reader :name
attr_reader :balance
def initialize(name, balance=100)
@name = name
@balance = balance
end
private
def pin
@pin = 1234
end
def pin_error
return "Access denied: incorrect PIN."
end
public
def display_balance(pin_number)
if pin_number === pin
puts "Balance: $#{@balance}."
else
puts pin_error
end
end
end
Private Affairs
#Codecademy sample solution
class Account
attr_reader :name
attr_reader :balance
def initialize(name, balance=100)
@name = name
@balance = balance
end
private
def pin
@pin = 1234
end
def pin_error
return "Access denied: incorrect PIN."
end
end
Codecademy: Creating the Account Class
#sample solution
class Account
attr_reader :name
attr_reader :balance
def initialize(name, balance=100)
@name = name
@balance = balance
end
end
Tuesday, January 22, 2013
Codecademy Sample Solution: Mixin For the Win
module Languages
FAVE = "Ruby" # This is what you typed before, right? :D
end
class Master
include Languages
def initialize; end
def victory
puts FAVE
end
end
total = Master.new
total.victory
Codecademy Sample Solution: Module Magic
# Create your module below!
module Languages
FAVE = "Javascript"
end
Codecademy Sample Solution: Private Affairs
class Application
attr_accessor :status
def initialize; end
# Add your method here!
public
def print_status
puts "All systems go!"
end
private
def password
return 12345
end
end
Codecademy Sample Solution: A Matter of Public Knowledge
class Application
attr_accessor :status
def initialize; end
# Add your method here!
public
def print_status
puts "All systems go!"
end
end
Codecademy Sample Solution: Imitating Multiple Inheritance
# Create your module here!
module MartialArts
def swordsman
puts "I'm a swordsman."
end
end
class Ninja
include MartialArts
def initialize(clan)
@clan = clan
end
end
class Samurai
include MartialArts
def initialize(shogun)
@shogun = shogun
end
end
#test
Leo = Ninja.new("Tutle")
Leo.swordsman
Himura = Samurai.new("Hogan")
Himura.swordsman
Monday, January 21, 2013
Codecademy Sample Solution: Feeling Included
class Angle
include Math
attr_accessor :radians
def initialize(radians)
@radians = radians
end
def cosine
cos(@radians)
end
end
acute = Angle.new(1)
acute.cosine
Codecademy Sample Solution: A Few Requirements
require 'date'
puts Date.today
Codecademy Sample Solution: Resolve to Keep Learning!
# Write your code below!
puts Math::PI
Codecademy Sample Solution: Module Syntax
module MyLibrary
FAVE_BOOK = "Bible"
end
Sunday, January 20, 2013
Codecademy Sample Solution: attr_accessor
class Person
attr_reader :name
attr_accessor :job
def initialize(name, job)
@name = name
@job = job
end
end
client = Person.new("Jim", "programmer")
puts "#{client.name} is a #{client.job}"
client.job = "dentist"
puts "#{client.name} is a #{client.job}"
Codecademy Sample Solution: attr_reader, attr_writer
class Person
attr_reader :name
attr_reader :job
attr_writer :job
def initialize(name, job)
@name = name
@job = job
end
end
client = Person.new("Jim", "programmer")
puts "#{client.name} is a #{client.job}"
client.job = "dentist"
puts "#{client.name} is a #{client.job}"
Codecademy Sample Solution: Private! Keep Out!
class Dog
def initialize(name, breed)
@name = name
@breed = breed
end
public
def bark
puts "Woof!"
end
private
def id
@id_number = 12345
end
end
Codecademy Sample Solution: Going Public
class Dog
def initialize(name, breed)
@name = name
@breed = breed
end
public
def bark
puts "Woof!"
end
end
#test
contestant1 = Dog.new("Snoopy", "Beagle")
contestant1.bark
Codecademy Sample Solution: Quick Review: Building a Class
class Dog
def initialize(name, breed)
@name = name
@breed = breed
end
end
Codecademy Sample Solution: Instantiation Nation
class Computer
@@users = {}
def Computer.get_users
return @@users
end
def initialize(username, password)
@username = username
@password = password
@files = {}
@@users[username] = password
end
def create(filename)
@filename = filename
@time = Time.now
@files[filename] = @time
puts "A new file was created: #{@filename} #{@username} #{@time}"
end
end
my_computer = Computer.new("Jim", test)
Subscribe to:
Posts (Atom)
This is an example of scrolling text using Javascript.
Popular Posts
-
//Sample Solution const gameState = {} function preload() { this.load.image('codey', 'https://s3.amazonaws.com/codecademy-...
-
//sample solution function guessNumber(number, clue) { // Prompt the user for a number using the string value of clue guess = promp...
-
//sample solution //Here is the original card object var card1 = {"suit":"clubs", "rank": 8}; //Create a...
-
#sample solution def colorful_conditions(): color = "blue" if color == "red": return "firs...
-
function checkAnswerWrongPlace(ans, realanswer){ // This method compares two input strings representing a player's guess ("an...
-
Instruction: Find the id of the flights whose distance is below average for their carrier. Sample Solution: SELECT id FROM flights AS f...
-
# note: The instructions state to use "text.txt" but an error comes up stating that the file does not exist # as a work around, ...
-
# Sample Solution # Import packages import numpy as np import pandas as pd # Read in transactions data transactions = pd.read_csv(...
-
# Sample Solution from song_data import songs import numpy as np q1 = np.quantile(songs, 0.25) #Create the variables q3 and interquarti...
-
/*sample solution for style.css*/ body > p { background: green; } p { background: yellow; }