Last active
December 11, 2015 11:08
-
-
Save pwightman/4591470 to your computer and use it in GitHub Desktop.
Some Ruby basics, given you have some experience with Java or similar.
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
# Declaring a class. You can subclass using: < SuperClass | |
class Fraction # < Foo | |
# Defines getters/setters | |
attr_accessor :numerator, :denominator | |
# Constructor | |
def initialize num, denom | |
# @ means it's an instance variable. Instance variables | |
# exist as soon as you use them. If you try to use an instance | |
# variable that doesn't yet exist, it just returns nil. | |
@numerator = num | |
@denominator = denom | |
end | |
# You can override the getter/setter created from attr_accessor | |
def numerator | |
puts "SURPRISE!" | |
end | |
# All methods take an implicit block, which can be | |
# called using "yield" | |
def with_fraction | |
if block_given? | |
yield(@numerator, @denominator) | |
end | |
end | |
# Like a "to string" method, but usually prints out very detailed, | |
# verbose information about an object, useful for debugging. | |
def inspect | |
#@numerator.to_s + "/" + @denominator.to_s | |
"#{@numerator}/#{@denominator}" | |
end | |
# The "to string" of Ruby. There's also to_i for converting to integer, | |
# to_f for converting to float, and more. | |
def to_s | |
"foo" | |
end | |
end | |
# Create a new Fraction object | |
f = Fraction.new 1, 2 | |
puts f | |
# Show passing a block into a method, see with_fraction for what happens | |
# when the method is called. | |
f.with_fraction do |num, denom| | |
puts "#{num} / #{denom}" | |
end | |
# Two forms for creating an array, Array.new is almost never used. | |
#arr = Array.new | |
arr = ["one", "two"] | |
# Accessing an array is as you'd expect | |
arr[0] | |
# Two forms for creating a Hash, Hash.new is also almost never used. | |
#hash = Hash.new | |
hash = { "baz" => "foo", 1 => "bar" } | |
# Accessing a hash is the same as an array, but you can use anything | |
# as the key. | |
hash["baz"] | |
# Your typical if/else. | |
if arr[0] == "foo" | |
puts "It was foo" | |
else | |
puts "It wasn't foo" | |
end | |
# More java-esque looping, not very Ruby-esque. | |
#for i in arr | |
#puts i | |
#end | |
# The "Ruby Way" of looping is to call "each" on an array/hash and | |
# pass in a block of code. The each method will call this block of | |
# code for every element in the array. The block is inside the | |
# do ... end portion, and blocks (like methods) can take parameters | |
# which is where the |i| comes in. How does this work? See below. | |
arr.each do |i| | |
puts i | |
end | |
# You can reopen any class at any time, override methods, add | |
# new ones, etc. This turns out to be a very interesting and | |
# controversial part of Ruby. | |
class Array | |
# each is a method already defined on Array, I just reimplemented | |
# it here to give you some insight into how passing blocks work. | |
def each | |
i = 0 | |
while i < self.count | |
yield(self[i]) | |
i += 1 | |
end | |
self | |
end | |
end | |
arr = ["foo", "bar"] | |
# Note that we're using the each method we defined just above. Try adding | |
# a print statement to confirm if you're unsure. | |
arr.each do |i| | |
puts i | |
end | |
# Iteration usually takes a block form, and this is another example. If you | |
# need to iterate some number of times, this is the prefer Ruby way of doing it. | |
# Also note that EVERYTHING in Ruby is an object. Everything. So you can call methods | |
# on numbers. | |
100.times do |i| | |
puts "Hi #{i}" | |
end | |
# You can actually save blocks (Procedures, hence Proc) into variables. Explicitly | |
# creating blocks and saving them in variables is not used very often in Rails code, | |
# though it is occassionally. | |
method = Proc.new do |arg| puts arg end | |
method.call("foo") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment