This file contains hidden or 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
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" |
This file contains hidden or 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
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| |
This file contains hidden or 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
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 |
This file contains hidden or 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
class Person | |
attr_reader :balance | |
def balance(balance) | |
@balance = balance | |
end | |
def balance_test? | |
p "Your balance measurement is: #{@balance}" | |
if @balance > 500 |
This file contains hidden or 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
# 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 |
This file contains hidden or 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
#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 |
This file contains hidden or 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
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 |
This file contains hidden or 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
I, [2015-11-11T12:43:21.813873 #53754] INFO -- random number: 7358696 | |
I, [2015-11-11T12:43:21.814242 #53754] INFO -- random number: 4563567 |
This file contains hidden or 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
class Charlie | |
def initialize(guitar) | |
@guitar = guitar | |
end | |
def play(guitar) | |
guitar.type == 'electric' ? guitar.sound.upcase : guitar.sound.downcase | |
end | |
end |
This file contains hidden or 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
# 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 |