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
-
# Codecademy sample solution # What are the four commonly used HTTP methods ("verbs")? # A: FIND, SEND, UPDATE, REMOVE # B: ...
-
$("#tours > li")
-
<!--sample solution--> <html> <head> <title>Week 13 Project</title> </head> ...
-
//sample solution def isEven(x): if(x % 2 == 0): return "yep" else: return "nope"
-
<html> <head> <!-- Add the Parse SDK here! --> <script src="http://www.parsecdn.com/js/par...
-
<!-- Sample solution to index.html --> <!DOCTYPE html> <html> <body> <div class="nav"> ...
-
Answer: Alt - p p stands for previous. If you keep pressing Alt - p, you will continue cycling back through commands you typed before. I...
-
#sample solution hungry = false unless hungry puts "I'm writing Ruby programs!" else puts "Time to eat!...
-
// sample solution to panel.js $(document).ready(function(){ var $panel = $("#panel"); var $tab = $("#tab"); ...
-
//sample solution var yourName = "Pam"; var gender = "female"; var result; //Line 10 starts an if statement //Ne...