Ruby

Integer methods

1.methods.sort

String methods

“”.methods.sort

String Interpolation

a = 1
b = 4
puts “The number #{a} is less than #{b}” => The number 1 is less than 4

def string_length_interpolater(incoming_string)
“The string you just gave me has a length of #{incoming_string.length}”
end

Search in a String

“[Luke:] I can’t believe it. [Yoda:] That is why you fail.”.include?(‘Yoda’)

Other:

  • .start_with?(“Ruby”)
  • .end_with?()
  • .index()
  • upcase
  • downcase
  • swapcase
  • split(‘ ‘)
  • concat() or +
  • sub(“findText||RegEx”, “replaceText”) – only replaces first occurrence
  • gsub(“findText||RegEx”, “replaceText”) – replaces all occurrences (ex gsub(/[A-Z]/,’0′))

Find a substring using RegEx

‘RubyMonk Is Pretty Brilliant’.match(/ ./)  => I

Adding to array

.push or <<

 

Transform Array

[1, 2, 3, 4, 5].map { |i| i*3} * note curly brace

Filter elements of an Array

[1,2,3,4,5,6].select {|number| number % 2 == 0}  * note curly brace

Blocks

addition = lambda {|a, b| return a + b }
puts addition.call(5, 6)

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s