Skip to content

Instantly share code, notes, and snippets.

View kkchu791's full-sized avatar

Kirk Chu kkchu791

  • Los Angeles, CA
View GitHub Profile
class Person
attr_accessor :balance
def balance_test
p "Your balance measurement is: #{@balance}"
case @balance
when 500...1000
"You could be a gymnast"
when 100...500
"You have normal balance"
def save_game(file)
score = 1000
open(file, "w") do |f|
f.puts(score)
f.puts(Time.new.to_i)
end
end
def load_game(file)
open(file, "r") do |f|
@kkchu791
kkchu791 / person.rb
Last active November 15, 2015 00:06
class Person
attr_accessor :balance
def balance_test?
p "Your balance measurement is: #{@balance}"
if @balance > 500
"You could be a gymnast"
elsif @balance > 100 && @balance < 500
"You have normal balance"
else
@kkchu791
kkchu791 / person.rb
Last active November 14, 2015 23:56
class Person
attr_reader :balance
def balance(balance)
@balance = balance
end
def balance_test?
p "Your balance measurement is: #{@balance}"
if @balance > 500
@kkchu791
kkchu791 / mixin1.rb
Last active November 15, 2015 21:52
# how do you mix module "Mixin" into class Charlie, such that I can call Charlie.method1?
module Mixin
def method1
"method1"
end
end
class Charlie
include Mixin
#Write a class called Person that has balance as an instance variable and make it readable via an accessor.
class Person
attr_reader :balance
def initialize
@balance = 400
end
end
class Logger
def self.log(random)
File.open("./logfile.txt", "a") { |f| f.puts "#{random}" }
end
end
class Charlie
def random_number
Logger.log(rand(10000000))
end
I, [2015-11-11T12:43:21.813873 #53754] INFO -- random number: 7358696
I, [2015-11-11T12:43:21.814242 #53754] INFO -- random number: 4563567
class Charlie
def initialize(guitar)
@guitar = guitar
end
def play(guitar)
guitar.type == 'electric' ? guitar.sound.upcase : guitar.sound.downcase
end
end
# I think the problem lies in class Charlie not being able to inherit the instance variables from class Guitar.
#For example, when we run this code:
class Guitar
def initialize(brand, color)
@color = color
@brand = brand