Ruby - Strings
|
|
|
“#{concate}#{nation}”
|
|
|
|
|
Single quotes string, auto escape characters.
Ruby - Array
|
Used for all examples below |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ruby - Block
|
Inline Block |
(1..5).each do |i| puts 2 * i end
|
Multi Line Block |
%w[A B C].map { |char| char.downcase }
|
%w[A B C].map(&:downcase)
|
The last 2 lines result in an array of ["a", "b", "c"]
. The %w creates an array of string. The last one is a weird Ruby shortcut.
|
|
Ruby - Conditionals
puts "string is not empty" if !astring.empty?
puts "string is not empty" unless astring.empty?
if <expression>
...
else
...
end if
|
Ruby - Hash + Symbol
foo = { "name" => "John", "id" => "JH" }
|
|
|
foo = { name: "John", id: "JH" }
|
|
|
foo.each do |key, value| puts "#{key.inspect} - #{value.inspect}" end
|
:name - "John" :id - "JH"
|
|
|
Ruby - Class
module BunchOfMethods
def moduleMethod1
...
end
def moduleMethod2
...
end
end
class MyClass < MySuperClass
attr_accessor :attributeA, :attributeB
def initialize(attributes = {})
@attributeA = attributes[:attributeA]
@attributeB = attributes[:attributeB]
end
def NiceString
"#{@attributeA} #{@attributeA}"
end
end
|
|