Cheat Sheet For Ruby
Brandon Moreno
Posted on November 21, 2022
Ruby is...
A dynamic object-oriented, general-purpose programming language with a focus on simplicity, just as every programming language was originally designed to solve some kind of problem. It was designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan. Matsumoto, often referred to as "Matz", first developed the idea for Ruby in the early 1990s.
With Ruby you can build different kinds of programs(Games, Command line interfaces, Web servers, and Server-side applications)
In this Article, we'll go over some commands that will help on your journey of learning ruby.
Basics
&&: and
||: or
!: not
(x && (y || w)) && z: use parenthesis to combine arguments
Commenting
# this line is commented out
IRB
$ irb
#To be able to write ruby in your terminal
Blocks and Procs
Depending on preference you can replace {}
with do end
and vice versa. Keep in mind, this will not work for hashes and #{}
escaping.
Blocks are not objects so, just is a bit code between do
, end
, or {}
. This can be passed to methods like .each, .select, and .find
Snake_Case
Rudy uses Snake Case.
Hello_World
or 10_000_00: 1000000.
Class
@
: instance variable
@
: class variable
Constant
Constants are used for values that aren't supposed to change and MOSTLY are all caps. Example:
FRUIT = "banana"
Arrays
this_is_an_array = [5, 4, 3, 2, 1]
If you see <<
it's just like a push method in JavaScript. [1,2,3] << 4 # [1,2,3,4]
Numbers
When using integer
it doesn't place decimals, but to put a number with decimals use float
.
CRUD
When you see CRUD it means create, read, update delete methods.
Booleans
At the end of a name if there is a question mark, it's a boolean. Example: empty?
, all?
, or match?
Hashes
hash = { "key1" => "value1", "key2" => "value2" } # same as objects in JavaScript
hash = { key1: "value1", key2: "value2" } # the same hash using symbols instead of strings
Symbols
:symbol
symbol is like an ID in html. :symbol != "symbol". Symbols are often used as Hash keys or referencing method names.
my_hash = { key: "value", key2: "value" } # is equal to { :key => "value", :key2 => "value" }
Methods
def say_hello(hello, *names) # *names is a split argument, takes several parameters passed in an array
return "#{hello}, #{names}"
end
start = say_hello("Steve", "Sam", "Maz", "
Dan The Man") # call a method by name
def name(variable=default)
### The last line in here gets returned by default
end
.times
47.times do
print “this text will appear 47 times”
end
.map() # is the same as .collect
Classes
class Person # class names are rather written in PascalCase (It is similar to camelCase, but the first letter is capitalized)
@@count = 0
attr_reader :name # make it readable
attr_writer :name # make it writable
attr_accessor :name # makes it readable and writable
def Methodname(parameter)
@classVariable = parameter
@@count += 1
end
def self.show_classVariable
@classVariable
end
def Person.get_counts # is a class method
return @@count
end
private
def private_method; end # Private methods go here
end
the_boy_maz = Person.new("Mazin")
the_boy_maz.show_name # Mazin
If
if 1 < 2
puts “one smaller than two”
elsif 1 > 2 # *careful not to mistake with else if. In ruby you write elsif*
puts “elsif”
else
puts “false”
end
String Method
"Hello".length # 5
"Hello".reverse # “olleH”
"Hello".upcase # “HELLO”
"Hello".downcase # “hello”
"hello".capitalize # “Hello”
Rake
rake db:create # Creates the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:create:all to create all databa...
rake db:drop # Drops the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all databases in...
rake db:fixtures:load # Load fixtures into the current environment's database
rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)
rake db:migrate:status # Display status of migrations
rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n)
rake db:schema:cache:clear # Clear a db/schema_cache.dump file
rake db:schema:cache:dump # Create a db/schema_cache.dump file
rake db:schema:dump # Create a db/schema.rb file that is portable against any DB supported by AR
rake db:schema:load # Load a schema.rb file into the database
rake db:seed # Load the seed data from db/seeds.rb
Side Note
sudo lsof -i :<port>
sudo kill -9 <port PID>
Kill a open port in OSX.
Thank you for taking the time to go over this article. Wish the best for you on your ruby journey.
Posted on November 21, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024