Skip to content

Instantly share code, notes, and snippets.

@M0119
Last active March 19, 2019 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save M0119/5dd39a0bce8482e3092fe32c5e4617a4 to your computer and use it in GitHub Desktop.
Save M0119/5dd39a0bce8482e3092fe32c5e4617a4 to your computer and use it in GitHub Desktop.

Lecture ✍️

String methods

Create a name string variable.

name = 'harrison'
name.upcase
name.downcase
name.capitalize

Get user input.

name = gets.chomp
puts "hi my name is #{name}"

Change string to number data type.

num = "1"
num.to_i
=> 1

Other ways to manipulate strings.

puts "harrison"
print "harrison"
# output to terminal as string

"harrison " + "malone"
# concatenate a string 

puts "my name is harrison\nmalone"
=> my name is harrison
malone
# non printing character, \n inserts line break in this case

We also briefly looked at math. Not the differences between integer and float division. The ** operator represents an exponent (to the power of).

1 + 1
# => 2

4 / 2
# => 2

5 / 2
# => 2

5 / 2.0
# => 2.5

2 ** 3
# => 8

To create a ruby comment you insert a hashtag # at the front of the line.

# this is a comment

The shortcut on macOS to do this is cmd /.

Resources

# 1. Describe in words the following code:
# this_var = 0
# this_string = "a string"
# x = 5.5
# badly_named = x * 7
# fergus = "christmas tree”
# nigel = "one " + " "two " + "three.”
# beer = fergus
# 2. Take the calculations from earlier, and store them in variables.
# That is, as you do the mathematics, store each line of the calculation in a variable,
# and use this on the next line to calculate the next step.
# 3. Use terminal to create a new file (e.g. `touch my-cool-app.rb`)
# 4. Open the file in your code editor (e.g. `atom my-cool-app.rb`)
# 5. Write the same Ruby code (1+1) and save + close the file.
# 6. Run the code! We can do this with the `ruby` command. (e.g. `ruby my-cool-app.rb`)
# 7. The code runs, but nothing is returned on the screen. Add `puts` to the start of your code and try running the code again! (e.g. `puts 1+1`)
# 8. Experiment with some basic ruby!
# 9. Use interpolation to put your name on the screen
# 10. Make a new variable called 'siblings', set its value to the number of siblings you have (integer) Use code to put your number of siblings on the screen. Use interpolation to pretty it up, E.g.: "Total Siblings: 3” Use code to increase your number of siblings by one.
# 11. Use interpolation again to put your new number of siblings on the screen
# - Check which version of ruby you have installed (if it isn’t 2.5.1, then please see a teacher)
# - Create a ruby folder inside of your apps folder (if you haven’t already done so)
# - Create a file inside the ruby folder called ‘strings’ with the ruby extension
# In IRB calculate:
# 1. How many hours are in a year?
# 2. How many minutes are in a decade?
# 3. How many seconds old are you?
# Try computing these in irb:
# Is the result a float or an integer?
# 1. 3.0 / 2
# 2. 3 / 2.0
# 3. 4 ** 2.0
# 4. 4.1 % 2
# Why is 4.1 % 2 => 0.099. Look up in the ruby docs or google modulo
# 1. `puts` "Hello world!" onto the screen
# 2. Make a new variable called 'name', set its value to your name (as a string)
# 3. Use interpolation to put your name on the screen
# 4. Make a new variable called 'siblings', set it's value to the number of siblings you have (integer) Use code to put your number of siblings on the screen. Use interpolation to pretty it up, E.g.: “Total Siblings: 3” Use code to increase your number of siblings by one.
# 5. Use interpolation again to put your new number of siblings on the screen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment