- Everything went pretty smooth. I feel that this will be easier once I am working on a Mac.
- $ atom.
- .rb
- cmd + \
- cmd + p
- pwd stands for "print working directory" and lets you know which directory you are currently in and its parents
- ls lists the files and child directories in the current directory. cd changes which directory you are in, and mkdir makes a new directory
- hostname tells you the name of the computor you are working on. The hostname for my codeacademy terminal online 2a3b1de782d1
- posted as comment
- To enter an irb session just type 'irb' into the terminal
- To square a number use "**2"
- 'puts'
- To run a ruby file from command line, use:'ruby filename.rb'. Include the file tree if it is not in the working directory
- '#' starts a comment line
- "turing".length returns 6, the number of characters in the string
- A pomodoro break is taken after each pomodoro period of work (usually 25 minutes)
- Variables are assigned using a single '='
- variables should be lower case, start with a character, and use underscores instead of spaces
- Variables can have their value changed by the same method they are assigned
- You can determine a variable's type by using the .class method
- .length will determine the length of a string and .include? will search a string for a specified value
- variable.to_s will convert the variable to a string
- Double quotes allow for interpolation while single quotes do not.
- #{} inserts a variable into a string
- variable.delete('aeiou')
- puts outputs a value to the terminal and starts a new line. print outputs a value to the terminal without starting a new line.
- gets gets user input as a string
- floats have a decimal while integers are whole numbers and do not
- 2**2
- Boolean Operators
- is equal to
- greater than or equal to
- less than or equal to
- not equal to
- AND
- OR
- .start_with? and .end_with?
- Flow control is the path a program takes while executing
- Not many apples...
- An infinite loop is created when executing a while or a do loop that doesn't have an end condition. ctrl + c should end the loop
- nil is Ruby's version of an empty value
- Symbols save memory by letting different variables point to the same object
- In most ways the rules are the same, but symbols can't be assigned values
- The .length method
- 0
- push adds a value to the end of an array, and pop calls the last value in the array
- An array is a list of values indexed by numbers. Hashes assign keys to values in a list to make them easier to index.
- I would use an array for a set of values that I don't need to access specific items. I would use a hash if I needed to pull specific items from the list.
- puts "Turing"[0]
- puts "Turing".length
- puts "Turing".upcase
- puts "Turing".delete("n")
- variable = "Turing" puts variable
- gets pauses the program and gets user input as a string
- chomp removes the line break at the end of the value
- the .to_i method converts the variable fab_num to an integer
- returns 4, the length of the array
- dog
- false
- .push() or <<
- .pop
class GuessingGame
@@game_tracker = []
def initialize()
@game_info = {
guess_count: 0,
cheat: false,
number: rand(100).to_i,
hint: false
}
puts "I have generated a random number for you to guess!"
end
def guess()
if @game_info[:guess_count] == 5 && @game_info[:hint] == false
hint()
end
print "What is your guess? > "
guess = gets.chomp
if guess =~ /cheat/ || guess == "c" #If it matches cheat then do cheat
@game_info[:cheat] = true
puts "That's cheating! The number is #{@game_info[:number]}. Guess again."
guess()
elsif guess.scan(/\D/).empty? #If it is all digits run test, move please guess to catch-all
guess = guess.to_i
@game_info[:guess_count] += 1
test(guess)
else
puts "Please guess a number."
guess()
end
end
def test(guess)
if guess == @game_info[:number]
win()
elsif guess > @game_info[:number]
puts "You guessed too high! Guess again."
guess()
elsif guess < @game_info[:number]
puts "You guessed too low! Guess again."
guess()
end
end
def win()
puts "You got it right!!"
puts "It took you #{@game_info[:guess_count]} guesses!"
if @game_info[:cheat] == true
puts "It's easy when you cheat!"
elsif @game_info[:guess_count] < 5
puts "That didn't take long!"
else
puts "That's a lot of guesses..."
end
@@game_tracker << @game_info
again()
end
def hint(number = @game_info[:number])
@game_info[:hint] = true
if number.even?
puts "The number is even!"
guess()
else
puts "The number is odd!"
guess()
end
end
def again()
print "Would you like to play again? Yes/No > "
again = gets.chomp
if again =~ /yes/i
game = GuessingGame.new()
game.guess()
else
puts "You played #{@@game_tracker.length} games!"
print "Would you like to see how you did? Yes/No > "
track = gets.chomp
tracker(track)
end
end
def tracker(track)
if track =~ /yes/i
@@game_tracker.each_with_index do |game_info, index|
puts "Game #{index + 1}: #{game_info}"
end
puts "Goodbye!"
exit(0)
else
puts "Goodbye!"
exit(0)
end
end
end
game = GuessingGame.new()
game.guess()
- I was able to get through the work comfortably. I took my time, and even re-did several lessons to make sure I understood the concepts.
- I am confident that I thoroughly understand the syntax of Ruby, and really enjoy coding.
- I am having some trouble understanding how information is stored. Specifically, when code outside a class creates a new instance of a class, where is that information stored?
- I am really excited to start working on multiple object programs. I have an idea for a game that builds a maze of rooms.
- Understanding variable scope and accessors is really important to me.
- If I assign a variable to a new instance of a class, how can I change the variable later?
- Mostly coding etiquette quetsions, like when should I define a new function vs use another nested if.
- I got the game to count both the number of guesses and gave the user an opportunity to play again.
- I had some trouble understanding instance vs local variables and how to access them from outside an object.
- I re-took several of the lessons and did some independant research to figure out how to get the method to work.
- I did all of the challenges to see if I could. I posted my favorite, the message challenge.
- variable = function(args)
- The initialize method is called when a new instance of a class is created. It acts as the "setup" method for the new instance. The new method is what creates a new instance of a class Class.new and it activates the initialize method in the class. Instance variables are variables that are available throughout the class methods, but are unique to each instance of the class.
- The walkthrough was pretty straight forward. I really enjoyed learning to use classes and am curious of how to take user input to create a new instance of a class.
Writing out what you expect a program to do in plain english, ordering it stepwise, and then programming each piece has by far been the most effective workflow I have used so far. For simple programs it is easy to remember what you want the program to do, but as the program gets more complex it is important to have a high level understanding of how the program operates. I also have taken to inlcuding a test.rb file in the directory I am programming in so that I can test small chunks of code outside of the main program to see if they conceptually do what I want them to.
- $ echo -n hello will print hello without a trailing new line.
- Ctrl-A moves the cursor to the beginning of the line, Ctrl-E moves the cursor to the end of the line, and Ctrl-U clears to the begginning of the line.
- Ctrl-L is the shortcut for the clear command and Ctrl-D exits the terminal.
- "cat" is short for concatenate and can join files, but is also used to print out the contents of a single file to the terminal. "diff" compares two files and returns the differences.
- Use *$ ls .txt to list all text files. To inlude hidden files use $ ls -a .txt
- Download a file using "curl" in conjunction with the URL address of the file.
- You can search through a file using "less" and "/" to search for the text string. Spacebar will move forward one page.
- "grep" can search for text strings in files, or can be used to search for processes by combining it with "ps"
github repo for encryption program: https://github.com/anon0mys/encryption
Railsbridge Loops Challenge