This file contains 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 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 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 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 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 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 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 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 UnpredictableString < String | |
def scramble(string) | |
string.split("").shuffle.join("") | |
end | |
end | |
sentence = UnpredictableString.new | |
p sentence.scramble("It was a dark and stormy night.") |
This file contains 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 Shape | |
def click | |
p rotate | |
play | |
end | |
def play | |
sound | |
end |
This file contains 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 | |
Inf = 1.0/0.0 | |
def balance_test | |
puts "Your balance measurement is: #@balance" | |
case balance | |
when 500...Inf | |
"You could be a gymnast" | |
when 100...500 |