Skip to content

Instantly share code, notes, and snippets.

@maatthc
Last active October 14, 2019 03:35
Show Gist options
  • Save maatthc/d0640cd0ff3fef0dcefb3e25b46f358f to your computer and use it in GitHub Desktop.
Save maatthc/d0640cd0ff3fef0dcefb3e25b46f358f to your computer and use it in GitHub Desktop.
# Ruby 2.3.3
# puts print
# Simbols are simpler String objs
# Constants are Capitalized variables
# Methods name finishing with a question mark returns booleans
# You can chain methods: job.get().askRaise()
# Global vars starts with a Dollar sign: $
# Instance vars starts with an At sign: @
# Class vars starts with an @@
# Block of codes are defined with Curly braces {} or "do"/"end"
# Block arguments are surronded by pipes: || and are used at the beginning of a code block {}
# Ranges are definied by parentheses and ellipsis: ('a'..'z')
# Arrays are defined by square brackets and separeted by commas: [1,2,3] - shift/unshift pop/push
# Hashes are dics that can use vars and arrows: {'a'=>1,'b'=>2}
# Hashes can use simbols: {a:1, b:2}
# Regexs are defined using slashes: /\A[0-9]/
# Pattern matching
puts (/test/ =~ "abcdefghijtestklmnoqrstuvwxyz") # prints 10
# Keywords are build-in words
# "if" returns: msg = if job
# "happy"
# else
# "sad"
# end
# gets().chomp read from Stdin - or $stdin.gets.chomp
# String.gsub!(a,b) substitutes "a" for "b" in the String
# File::open() doesnt need include
# strip() remove leading and trailing whitespaces
# All keywords/methods come from the super class Kernel! E.g. Kernel::gets()
# Dir['lixo*']{|file| print file}
# Adv sort : kitty_toys.sort_by { |toy| toy[:fabric] }
name = 'maat'
puts "#{name} eh foda" + ".. mas sera mesmo?" * 10
puts 2.to_s()
formatter = "%{d} %{b} - %{c} %{a}"
puts formatter % {a:'1', b:'2', c:'3', d:'4'}
test = %q{ one
two
three
}
another = """ 1
2
\t3
\a
"""
puts test + another
puts ARGV.size
if File.exist?($0) # This script
puts File.basename(__FILE__, File.extname($PROGRAM_NAME))
elsif false
puts "never"
else
puts "nops"
end
def many(*lots)
for one in lots
puts one
end
lots
end
what = many('aa','bb')
puts what
ARGV.each do |arg|
print arg
end
mystuff = {"apple" => "Do I like apple?"}
puts mystuff['apple']
class Animal
def initialize(race)
@race = race
end
attr_reader :race
def describe()
puts "This is a #{race} Animal!"
end
end
class Cat < Animal
def initialize()
@race = "Cat"
end
end
thing = Animal.new('dog')
puts thing.race
thing.describe()
cat = Cat.new()
cat.describe
PHRASE_FIRST = ARGV[0] == "english"
puts PHRASE_FIRST
# More here: https://scotch.io/tutorials/a-crash-course-in-ruby
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment