A compendium of obscure Ruby methods (or maybe just a couple methods/concepts that I didn’t know before…)

Some argument options:

def print_from_array((a1,a2))
print first: a1, second: a2
end
print_from_array(['output1', 'output2'])
=> {first: 'output1'}, {second: 'output2'}
def print_from_array((a1,a2))
print first: a1, second: a2, third: a3
end
print_from_array(['output1', 'output2', 'output3'])
=> NameError (undefined local variable or method `a3' for main:Object)
def convert_args_to_array(*args)
print args
end
convert_args_to_array("string1", "string2", "string3")
=> ["string1", "string2", "string3"]
def convert_some_args_to_array(arg1, *args)
print arg1, args
end
convert_some_args_to_array("string1", "string2", "string3")
=> string1 ["string2", "string3"]

2. Some array methods:

array1 = [1,2,3,4,5,6,7,8,9] 
array2 = [1,2,3,4,'a','b','c']
array3 = ['a','b','c','d','e']
array1 & array2 => [1,2,3,4]
array2 & array3 => ['a','b','c']
array1 & array3 => []
array1 = [1,2,3,4,5,6,7,8,9] 
array2 = [1,2,3,4,'a','b','c']
array3 = ['a','b','c','d','e']
array1 - array2 => [5,6,7,8,9]
array2 - array1 => ['a','b','c']
array2 - array3 => [1,2,3,4]
array3 - array2 => ['d','e']
array = [1,2,3,4,5]
array.rotate => [2,3,4,5,1]
array = [1,2,3,4,5]
array.shuffle => [5, 4, 2, 1, 3]
array.shuffle => [3, 4, 5, 1, 2]
array.shuffle => [5, 1, 3, 2, 4]

3. Proc.new (or ‘proc’) & Lambda (->{code block}):

add_one = proc {|number| number + 1}
add_one.call(5) => 6
p = proc {|x, y| x + y }

[[1, 2], [3,4]].map(&p) #=> [3, 7]

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Flectra ERP Details

Flectra ERP Details

Understanding stream processing

Signed up for Kodecamp!

Using Git Like A Ninja

Intro to Object-Oriented Programming For Data Scientists

UNITY GAME DEVELOPER SKILLS & PROFICIENCIES TO CONSIDER BEFORE HIRING THE ONE

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yisrael Gurkow

Yisrael Gurkow

More from Medium

A Complete Guide To Regular Expressions

Pair Programming

How to Setup GitHub “Pages” and Shave Hours Off of Project-Prep

Animated source code statistics with Vizzu