class Document
attr_accessor :content, :title
def initialize content = "", title = ""
@content = content
@title = title
end
def to_s
"#{title}: #{content}"
end
end
doc = Document.new
doc.title = "Ruby Exam"
doc.content = "TODO some questions"
puts doc.title
puts doc.content
puts doc
- Use attr_accessor instead of writer so you can read and assign to content and title
- add default parameter values to constructor so you can create a Document without any parameters
- initialise -> initialize
class RubyExam
def initialize
@score = 0
@current_question = 1
end
def got_one_right
@score++
next
end
def got_one_wrong
@score--
next
end
def next
@current_question++
end
def redo
puts "I made no mistake!"
@score = 10
end
def complete
if @score < 10
puts 'Game Over'
else
redo
end
puts "Final Score: #{@score}"
end
end
exam = RubyExam.new
exam.got_one_right
exam.got_one_wrong
exam.got_one_wrong
exam.complete
Module -> Class
The redo and complete methods feel odd but they work @_@
- {}
- Hash.new
- [].to_h (this is the best i got, no clue for a third way)
def save!(array)
raise ArgumentError.new('arg should be array') unless array.is_a?(Array)
# do stuff ..
rescue __X__
puts 'Failed to save!'
puts $1
puts $1.message
puts $1.backtrace
puts $@
end
-
Error => e
- nothing
-
SystemError => e
-
ArgumentError => e
-
StandardError => e
-
ArgumentError
-
=> ArgumentError
-
=> argument_error
d = Date.parse('01-02-99') << 1
p d.to_s
TOTAL GUESS
-
"1999-01-02"
-
"1998-01-01"
-
"1998-12-31"
-
"1998-12-01"
- an exception
d = Date.parse('2019-01-31') >> 1
p d.to_s
TOTAL GUESS
-
"2018-12-31"
-
"2020-01-31"
-
"2019-02-31"
-
"2019-02-28"
- an exception
toybox = { "doll" => 'Sally' }
-
toybox.doll
-
toybox['doll']
-
toybox["doll"]
-
toybox[:doll]
-
toybox.try('doll')
# Hmmmmmmmmmmmmmmmmmmmm feels like this might be a thing
- .ts
- .ts.erb
- .tsx
- .tsx.erb
/^\.(ts|tsx)?(\.erb)?$/
Here is a good editor for regexp, maybe try and do it without using it first tho: https://rubular.com/
I wasn't able to do this without the cheat sheet from that editor but this is my cheat assisted answer:
^\.(ts|tsx)(\.erb)?$
The problem was the first ? which allowed zero matches of (ts|tsx) (matching ., ..erb, .erb)
# /User/gollum/precious/ring.rb
File.dirname(__FILE__)
- 'precious'
# /User/tom/junk/ring.rb
File.path(__dir__)
- '/User/tom/junk/'
bark { "Oki" }
=> "Oki yips!"
def bark
puts "#{yield} yips!"
end
teams = [['greg', 'bob']]
teams << if teams.size < 2
['jim', 'jerry']
elseif teams > 2
['sam']
else
['tony']
end
p teams.flat_map { |t| t }
elseif should be elsif then the result is: '['greb', 'bob', 'jim', 'jerry']'