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
a = [5, 3, 4, 1] | |
a.sort # => [1, 3, 4, 5] | |
a.any? {|i| i > 6} # => false | |
a.any? {|i| i > 4} # => true | |
a.all? {|i| i > 4} # => false | |
a.all? {|i| i > 0} # => true | |
a.collect {|i| i * 2} # => [10, 6, 8, 2] | |
a.select {|i| i % 2 == 0 } # even => [4] | |
a.select {|i| i % 2 == 1 } # odd => [5, 3, 1] | |
a.max # => 5 |
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
a = [5, 3, 4, 1] | |
a.inject(0) {|sum, i| sum + i} | |
# => 13 | |
a.inject {|sum, i| sum + i} | |
# => 13 | |
a.inject {|product, i| product * i} | |
# = 560 | |
a.inject(0) do |sum, i| | |
puts "sum: #{sum} i: #{i} sum + i: #{sum + i}" | |
sum + i |
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
class Department < ActiveRecord::Base | |
has_many :employees | |
has_one :manager | |
end |
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
class NilClass | |
def blank? | |
true | |
end | |
end | |
class String | |
def blank? | |
self.size == 0 | |
end |
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
class Roman | |
def self.method_missing name, *args | |
roman = name.to_s | |
roman.gsub!("IV", "IIII") | |
roman.gsub!("IX", "VIIII") | |
roman.gsub!("XL", "XXXX") | |
roman.gsub!("XC", "LXXXX") | |
( | |
roman.count("I") + | |
roman.count("V") * 5 + |
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
class Person | |
attr_accessor :name | |
def self.can_speak # This is a class method! Notice the self. | |
define_method 'speak' do # an instance method | |
puts "I can talk, my name is #{@name}!" | |
end | |
end | |
def initialize(name) | |
@name = name |
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
module Person | |
attr_accessor :name | |
def self.included(base) # included is invoked whenever a module is included; base is implicit | |
base.extend ClassMethods # extend will add the methods defined in ClassMethods as class methods | |
end | |
module ClassMethods | |
def can_speak | |
include InstanceMethods # This includes all the instance methods | |
end | |
end |
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
class Tree | |
# Define instance variables children and node_name | |
# and the associated getters and setters | |
attr_accessor :children, :node_name | |
# Define a constructor with 2 parameters, with the second having a default value | |
def initialize(name, children=[]) | |
@children = children | |
@node_name = name | |
end |
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
1 + 2 // 2 | |
1e2 * 2.5 // 250 | |
Number.MAX_VALUE / Number.MIN_VALUE // Infinity | |
Infinity - Infinity // NaN | |
-Infinity + NaN // NaN | |
Math.log(Math.pow(Math.E, 200)) // 200 |
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
"hello".charAt(0) // h | |
'cat'.charAt(1) == 'cat'[1] // true | |
"hello, world".replace("hello", "goodbye") // goodbye, world | |
"hello".toUpperCase() // HELLO | |
'Duke PL Course'.substring(5,7) // 'PL' | |
"1, 2, 3".split(', ') // ['1', '2', '3'] | |
'Duke PL Course'.indexOf('Courses') // -1 |