Ruby
If you're looking for a programming language that values productivity over optimization, then Ruby is definitely for you.
- Ruby syntax is intuitive and has striking resemblance to the English language.
- So, if you are like me and did poorly in English class as a child, this is a win-win to brush up on grammar rules and Ruby.
- Otherwise, have a fun time learning Ruby and mastering it in no time.
Getting Started
# macOS
brew install ruby
# Debian
apt install ruby-full
There's also some debate as to what the canonical implementation of Ruby is this days. I've seen JRuby become more popular, so I've included the steps to install it on macOS below:
curl -sSL https://get.rvm.io | bash -s stable
<<-EOF
# Source ruby environment command
if [[ -e ~/.rvm/scripts/rvm ]]; then
source ~/.rvm/scripts/rvm
fi
EOF >> ~/.zshrc
source ~/.rvm/scripts/rvm
rvm install jruby
Input/Output
aka I/O - it's not rocket science, just computer science
Basic input is read in from the command line
var_name = gets.chomp #command line input
There are two types of print statements in ruby:
- print - no newline
- puts - adds a newline after output
var_string = "Welcome to the wiki"
print var_string #1. no newline
puts var_string #2. w/ newline
String Manipulation
Ruby includes given methods for various string manipulation techniques. Calling any of the following methods with the last character as '!' modifies the string in place vs. creating a copy.
- Create uppercase string - .upcase
to_upper = "John".upcase
to_upper.upcase!
- Create lowercase string - .downcase
to_lower = "Cameron".downcase
to_lower.downcase!
- Create reverse string - .reverse
rev = "Austin".reverse
rev.reverse!
- Create string with first char capitalized- .capitalize
first_capped = "Chase".upcase
first_capped.upcase!
Conditional Statements
if
/elsif
/else
blocks check if statements evaluate as true.
a = "Yes"
b = "No"
if a == "Yes"
print "a wins"
elsif b == "No"
print "b wins"
else
print "NGL ... so confused RN Bruh"
end
unless
/else
blocks check if statements evaluate as false.
a = false
unless a
print "a loses"
else
print "a wins"
end
Ruby also supports ternary conditional expressions and case statements. The following three code blocks are identical in practice.
In my opinion, ternary conditional expressions tend to have a cleaner syntax.
#global variable for consistency --> Not Good in Practice
a = true
#Classic Conditional Expression
if a == true
print "a wins"
else
print "a loses"
end
#Ternary Conditional Expression
print a == true ? "a wins" : "a loses"
#Case Conditional Statement
case a
when a == true
puts "a wins"
else
puts "a loses"
end
Data Structures
Coming Soon Friends...
Managing Gems
Updating Gems
Check if any gems need to be updated
gem outdated
gem update <gem_name>
By default, ruby will keep old versions of gems, so after updating, it's good to run the cleanup
command
gem cleanup
Uninstalling Gems
Find where a gem is installed
gem which <gem_name>
Uninstall a single gem
gem uninstall <gem_name>
Uninstall all gems with no confirmation
gem uninstall -aIx
Bundler
Nowadays bundler is built into Ruby 2.6, so keep on using it.
Jekyll
Previewing the website
It's a good idea to make sure the website looks correct before pushing your repository to GitHub. You can do that by running the following command inside the root directory of your project
bundle exec jekyll serve
You can close the server by issuing the following command in any directory
pkill jekyll
Variable Substitution
You can substitute variables inside of strings using the #{varname}
syntax:
name = "tommy"
puts "Hello, #{name}!"
# => `Hello, tommy!`
Heredoc
# Use `<<-` to preserve leading whitespace
puts <<-EOF
one
two
three
four
five
EOF
Since Ruby 2.3, the <<~
heredoc strips leading whitespace
# Use `<<~` to strip leading whitespace
def make_doc(body)
<<~EOF
<html lang="en-US">
<body>
#{body}
</body>
</html>
EOF
end