Skip to content

Instantly share code, notes, and snippets.

@havenwood
Forked from tylerneylon/learn.lua
Last active March 16, 2016 11:15
Show Gist options
  • Save havenwood/5871558 to your computer and use it in GitHub Desktop.
Save havenwood/5871558 to your computer and use it in GitHub Desktop.
Learn Ruby: A fork of Learn Lua (https://gist.github.com/tylerneylon/5853042)
# An octothorp (#) starts a single-line comment.
=begin
Starting with a =being and ending with a =end
makes it a multi-line comment.
=end
##
# 1. Variables and flow control.
#
num = 42
s = 'walternate' # Mutable strings.
t = "double-quotes are also fine"
u = %[ A precent sign can be followed by a delimiter
such as square brackets to start and end
multi-line strings.]
t = nil # Sets t to nil, does not dereference.
# Blocks are denoted with keywords like do/end:
while num < 50 # The `do` here is implicit.
num += 1 # No ++ type operator.
end
# If clauses:
if num > 40
puts 'over 40'
elsif s ~= 'walternate' # ~= is not equals.
# Equality check is == like Python; ok for strs.
puts "not over 40\n" # Defaults to stdout.
else
# Local variables are scoped to a method.
this_is_local = 5 # Snake case.
# An instance variable begins with an @:
@line = gets # Reads next stdin line.
# String concatenation uses the << operator:
puts 'Winter is coming, ' << line
# Or interpolation is idiomatic:
puts "Winter is coming, #{line}" # Double quotes allow interpolation inside #{}.
end
# Undefined variables return a NameError.
# This is an error:
foo = an_unknown_variable
#=> NameError: undefined local variable or method `an_unknown_variable'
a_bool_value = false
# Only nil and false are falsy; 0 and '' are true!
puts 'twas false' unless a_bool_value # Use 'unless' instead of 'if not'.
# 'or' and 'and' are short-circuited.
# This is similar to the a?b:c operator in C/js:
and_example = a_bool_value and 'yes' or 'no'; #=> 'no'
# Because of precedence differences, it is often
# advised to use &&/|| instead of and/or.
# Enumerating from 1 up to 100:
karl_sum = 0
1.upto(100) do |i|
karl_sum += i
end
# Another way, enumerating from 0 up to 99:
100.times do |i|
karl_sum += i
end
# Enumerating down from 100 to -1:
fred_sum = 0
100.downto(-1) do |j|
fred_sum += j
end
# Another loop construct:
until num == 0
puts 'the way of the future'
num -= 1
end
##
# 2. Methods and lambdas.
#
# Methods are more commonly used than lambdas:
def fib1(n)
return 1 if n < 2
fib(n - 2) + fib(n - 1) # Implicit return of the last line.
end
fib1(5)
#=> 8
fib1 5 # Parens areound method argument are often optional:
#=> 8
# Lambdas are anonymous closures in the current scope
# that can be assigned to variables and passed around.
# Lambdas have a different syntax than methods:
fib2 = ->(n){
return 1 if n < 2
fib2[n - 2] + fib2[n - 1]
}
# Lambdas are called with [] or .() or .call(), not () like methods.
fib2[5]
#=> 8
fib2.(5)
#=> 8
def adder(x)
# A lambda that remembers the value of x
# is created when adder is called:
->(y){ x + y }
end
a1 = adder(9)
a2 = adder(36)
a1[16] #=> 25
a2[64] #=> 100
# Assignments work with lists that are mismatched in length.
# Unmatched receivers are nil;
# unmatched senders are discarded.
x, y, z = 1, 2, 3, 4
# Now x = 1, y = 2, z = 3, and 4 is thrown away.
a, b, c = 1, 2
# Now a = 1, b = 2, and c = nil.
# Methods strictly insist by default that you
# call them with the correct number of arguments:
def bar(a, b, c)
[a, b, c]
end
# The wrong number of arguments raises an error:
bar
#=> ArgumentError: wrong number of arguments (0 for 3)
bar 1, 2, 3, 4
#=> ArgumentError: wrong number of arguments (4 for 3)
bar 1, 2, 3
#=> [1, 2, 3]
# Method arguments prefixed with a splat (*) are optional
# and also take unlimited additional arguments that are
# returned as an array:
def bar(a, *b)
[a, b]
end
# Arguments without a splat are still mandatory:
bar
#=> ArgumentError: wrong number of arguments (0 for 1+)
bar 1
#=> [1, []]
bar 1, 2, 3, 4
#=> [1, [2, 3, 4]]
#####
### This ^ seems to be end of direct correlation with the Lua gist.
### TODO: Add Ruby-specific parts including filling in Arrays and Hashes sections below:
#####
##
# 3. Arrays.
#
# Arrays are Ruby's basic lists.
# Arrays are denoted by square brackets [].
# A single array can contain a number of datatypes:
array = [42, 3.14, 'marmots', ['a nested array']]
array.size
#=> 4
# Arrays can be queried by index:
array[1]
#=> 3.14
array[0]
#=> 42
# Or check for inclusion:
array.include? 'weasels'
#=> false
array.include? 'marmots'
#=> true
# Iterate over arrays with the #each method:
array.each do |e|
puts e
end
##
# 4. Hashes
#
# Hashes are Ruby's primary dictionary with keys/value pairs.
# Hashes are denoted with squiggly braces:
hash = {'color' => 'green', 'number' => 5}
hash.keys
#=> ['color', 'number']
# Hashes can be quickly looked up by key:
hash['color']
#=> 'green'
hash['number']
#=> 5
# Asking a hash for a key that doesn't exist returns nil:
hash['nothing here']
#=> nil
# Iterate over hashes with the #each method:
hash.each do |k, v|
puts "#{k} is #{v}"
end
@adambard
Copy link

Hey there,

When you get done with this, I'd be super happy to put it on http://learnxinyminutes.com. The git repo is at https://github.com/adambard/learnxinyminutes-docs

@havenwood
Copy link
Author

@adambard Neat resource, checking out the closure one now. :) Adding a Ruby one sounds good. I'll poke you when I get it done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment