Skip to content

Instantly share code, notes, and snippets.

@rhysemmas
Created January 3, 2020 14:53
Show Gist options
  • Save rhysemmas/4106219a65521ef2cf523d10636d36b6 to your computer and use it in GitHub Desktop.
Save rhysemmas/4106219a65521ef2cf523d10636d36b6 to your computer and use it in GitHub Desktop.
Some important Ruby things & idioms discovered on my travels
# frozen_string_literal: true
# https://stackoverflow.com/questions/37799296/what-does-the-comment-frozen-string-literal-true-do
##############################
# Variables
##############################
# variables are just variables, there isn't anything weird going on here
my_var = "eggs"
# but, we can reference variables directly in strings with '#{}', which is cool
puts "Please add #{my_var} to the shopping list"
#Please add eggs to the shopping list
##############################
# Instance variables
##############################
def function1
# this is an instance variable, it is magic
@hello = "Hello"
end
def function2
puts @hello
end
function2()
#Hello
# see? magic
# actual details on instance variables:
# https://www.rubyguides.com/2019/07/ruby-instance-variables/ 
##############################
# Arrays & Iterating
##############################
fruit = []
# there are many ways to add to an array, but, we can easily append values to the fruit array using `<<`
fruit << "Apple"
# => ["Apple"]
# and because we are using Ruby, we can do weird looking things like this:
fruit << "Banana" << "Orange"
# => ["Apple", "Banana", "Orange"]
# there are multiple ways to iterate over an array, but the main ways are with the map or each methods
# 'map' is used to transform data, whereas 'each' just gives you each element, but we will get to that
# we can iterate over the array in the following way:
fruit.each { |f| puts f }
#Apple
#Banana
#Orange
# if we wanted to change (or transform) the elements in the array, we would use 'map'
big_fruit = fruit.map { |f| f.upcase }
# => ["APPLE", "BANANA", "ORANGE"]
# trying to do the same with 'each' won't work, as 'each' cannot transform data
big_fruit = fruit.each { |f| f.upcase }
# => ["Apple", "Banana", "Orange"]
# map method (also covers map vs each):
# https://www.rubyguides.com/2018/10/ruby-map-method/
##############################
# The &:method Idiom
##############################
# consider the fruit array in the last example, let's add an empty string:
fruit << ""
# => ["Apple", "Banana", "Orange", ""]
# we could remove the empty string by iterating over the array with the 'reject' method
# and use the 'empty?' method on each element to check whether the element is an empty string
clean_fruit = fruit.reject { |f| f.empty? }
# => ["Apple", "Banana", "Orange"]
# there is a shorthand way of doing this, which is in the format: array.method(&:method)
clean_fruit = fruit.reject(&:empty?)
# => ["Apple", "Banana", "Orange"]
# more details on &:method in the Further Reading section
# reject method in case you were interested:
# https://apidock.com/ruby/Array/reject
##############################
# Further Reading
##############################
# for more idioms, this is a bit advanced but has a funny title:
# https://coderwall.com/p/qfh2ua/ruby-tricks-to-make-your-code-more-fun-and-less-readable
# more links on &:method which I have included here, because it is complicated and in-depth knowledge is not required
# https://andrewjgrimm.wordpress.com/2011/10/03/in-ruby-method-passes-you/
# https://stackoverflow.com/questions/1217088/what-does-mapname-mean-in-ruby
# and read everything on www.rubyguides.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment