Last active
September 16, 2015 06:02
-
-
Save ghiculescu/264e78233a48d2d2a689 to your computer and use it in GitHub Desktop.
ruby workshop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# welcome to Ruby. i'm a comment. | |
# > irb | |
# `irb` is the command line ruby console. it opens up a REPL - an interactive terminal that you can evaluate ruby statements in. similar to python's `python`. | |
# in Cloud9, choose "Runner: Shell command" from the dropdown at the bototm (near CWD). Then run "irb" as your command. | |
1 + 1 | |
# => 2 | |
"hello world" | |
# => "hello world" | |
"hello world".class | |
# => String | |
"hello world".upcase | |
# => "HELLO WORLD" | |
"hello world".upcase() | |
# => "HELLO WORLD" | |
"everything is a method".reverse | |
# => "dohtem a si gnihtyreve" | |
"everything is an object".object_id | |
# => 18937180 | |
"hello world".gsub("hello", "goodbye, cruel") | |
# => "goodbye, cruel world" | |
variable = "boo" | |
"boo" == "urns" | |
# => false | |
array = [] | |
array << 1 | |
array << 2 | |
array | |
# > [1, 2] | |
array << "technically objects of any type can go into an array, but that's usually a pretty bad idea" | |
# => [1, 2, "technically objects of any type can go into an array, but that's usually a pretty bad idea"] | |
array.pop | |
# => "technically objects of any type can go into an array, but that's usually a pretty bad idea" | |
array | |
# > [1, 2] | |
array.nil? | |
# => false | |
array.empty? | |
# => false | |
[].empty? | |
# => true | |
nil.nil? | |
# => true | |
nil.empty? | |
# NoMethodError: undefined method `empty?' for nil:NilClass | |
hash = {} | |
hash[:brown] = 'tasty' | |
hash | |
# => {:brown=>"tasty"} | |
hash[:brown] | |
# => 'tasty' | |
hash['string key'] = 'you can use different types as keys' | |
hash['string key'] | |
# => 'you can use different types as keys' | |
# but it's usually a bad idea. you should use symbols unless you have a good reason. symbols start with colons, like in the example just below. | |
first_hash = {omg: "wow", key: "value", convoluted: "example"} | |
second_hash = {:omg => "wow", :key => "value", :convoluted => "example"} | |
first_hash == second_hash | |
# => true | |
# two ways to write the same thing - you'll see both, but should use the first whenever you can | |
hackers = ['alex', 'dave', 'jun', 'adam', 'nick'] | |
hackers.each do |name| | |
puts(name) # print each name out one by one | |
end | |
hackers.reverse.each_with_index do |name, index| | |
puts "Hacker ##{index + 1}: #{name}" # tanda competence ranking | |
end | |
# you always use .each to iterate over things. it's the equivalent of `for` in other langauges. `for` also exists in ruby but is slightly different and internally implemented as .each | |
hackers.map {|h| h.upcase.reverse} | |
# => ["XELA", "EVAD", "NUJ", "MADA", "KCIN"] | |
hackers | |
# => ["alex", "dave", "jun", "adam", "nick"] | |
hackers.map! {|h| h.upcase.reverse} | |
# => ["XELA", "EVAD", "NUJ", "MADA", "KCIN"] | |
hackers | |
# => ["XELA", "EVAD", "NUJ", "MADA", "KCIN"] | |
# "bang" methods (methods with exclamation marks) are generally methods that are "destructive" - they modify the object they are being called on, rather than returning a new, different one | |
# eg. Array.map!, String.chomp!, many many more | |
hash_that_you_can_iterate_over = {omg: "wow", key: "value", convoluted: "example"} | |
hash_that_you_can_iterate_over.each do |key, value| | |
puts(key) # :omg | |
puts(value) # "wow" | |
end | |
class Animal | |
attr_accessor :name # define getters and setters | |
def initialize(name) | |
@name = name # set instance variable | |
end | |
def human? | |
false # implicit return | |
end | |
def talk! | |
raise NotImplementedError.new # "throw an exception" | |
end | |
end | |
class Cat < Animal | |
def talk! | |
puts "meow" # prints to stdout | |
end | |
end | |
cat = Cat.new("Dave") | |
cat.human? # false | |
cat.talk! # "meow" | |
puts "i am printing something to standard output (it'll probably show up on your screen, but it could also write to a logfile if you configure that)" | |
puts "" # line break | |
puts "what is your name?" | |
name = gets # read from standard input. this would prompt the user of the program to type something in to the command line. | |
name | |
# => "....\n" | |
name.chomp! | |
name | |
# => "...." | |
puts "what is your quest?" | |
quest = gets.chomp # http://ruby-doc.org/core-2.2.0/String.html#method-i-chomp | |
puts "what is the airspeed velocity of an unladen swallow?" | |
velocity_answer = gets.chomp | |
correct_answer = velocity_answer == "european or american?" | |
puts "you are #{correct_answer ? "correct" : "incorrect"}!" | |
text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." | |
File.open("filename.txt", "wb") {|f| f.write(text)} | |
# File.open gives you a "block". This is like a callback in some functional languages. | |
# Basicaly the code inside the block (between the { and }) gets run at a specfied later time - in this case, when the file has been opened for writing. | |
text_read_from_file = File.read("filename.txt") | |
# => "Lorem Ipsum..." | |
`open https://gist.github.com/ghiculescu/97fbfe97c5abd77dd359` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment