Show off your favorite line method / function in ruby / rails.
Leave a ⭐️ if you find this helpful.
def make_me_a_hash(arr)
Hash[(0...arr.size).zip arr]
end
arr = ["one", "two", "three", "four", "five"]
make_me_a_hash(arr) # {0=>"one", 1=>"two", 2=>"three", 3=>"four", 4=>"five"}
def is_equal(arr1, arr2)
arr1.sort == arr2.sort
end
is_equal([1,2,3], [2,3,1]) # true
is_equal([1,2,3], [2,3,1,4]) # false
dup_array = [1,2,3].dup
# dup_array = [1,2,3]
a=\*(1..10)
Array (1..10)
(1..10).to_a
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
("a".."f").to_a
# ["a", "b", "c", "d", "e", "f"]
[:a, :b].product [:c,:d]
# [[:a, :c], [:a, :d], [:b, :c], [:b, :d]]
a.clear
def find_closest(n, arr)
arr.min_by{|x| (n-x).abs}
end
arr = [20, 30, 45, 50, 56, 60, 64, 80]
find_closest(40, arr) # 45
arr.index(arr.max)
arr = [20, 30, 45, 50, 56, 60, 64, 80]
arr.index(arr.max) # 7
arr.index(arr.min)
arr = [20, 30, 45, 50, 56, 60, 64, 80]
arr.index(arr.max) # 0
def find_longest_length(arr)
arr.max_by(&:length)
end
arr = ['one','two','three','four','five']
find_longest_length(arr) # "three"
def cumulative_sum(arr)
sum = 0
arr.map{|x| sum += x}
end
arr = [1, 2, 3, 4]
cumulative_sum(arr) # [1, 3, 6, 10]
c = [[["a"]], [["b"]]]
c.flatten # ["a", "b"]
c.flatten(1) # [["a"], ["b"]]
def get_consecutives_arrays(arr, n)
arr.each_cons(n).to_a
end
arr = [1, 2, 3, 4]
get_consecutives_arrays(arr, 2) # [[1, 2], [2, 3], [3, 4]]
get_consecutives_arrays(arr, 3) # [[1, 2, 3], [2, 3, 4]]
def array_average(arr)
arr.sum(0.0) / arr.size
end
arr = [1, 2, 3, 4, 5, 6, 7, 8]
array_average(arr) # 4.5
x = [1, 1, 2, 4]
y = [1, 2, 2, 2]
# intersection
x & y # [1, 2]
# union
x | y # [1, 2, 4]
# difference
x - y # [4]
def numbers_rank(arr)
rank = 1
arr.each_with_index.map{|value, i| arr[i-1] == value ? rank : rank = i+1}
end
arr = [89, 52, 52, 36, 18, 18, 5]
numbers_rank(arr) # [1, 2, 2, 4, 5, 5, 7]
arr.uniq
arr = [1,1,1,2,3,4,5]
arr.uniq # [1, 2, 3, 4, 5]
arr # [1,1,1,2,3,4,5]
def intersperse_element(arr, element)
arr.flat_map { |x| [x, element] }[0...-1]
end
arr = [1, 2, 3]
intersperse_element(arr, :a) # [1, :a, 2, :a, 3]
arr.partition { condition }
(1..6).partition { |v| v.even? } # [[2, 4, 6], [1, 3, 5]]
arr - [ nil ] # removes nil values
array.compact #removes nil values
arr - [ false ] # removes false
arr - [ nil, false ] #removes nil and false
arr.select(&:itself) # removes nil and false
arr = [0, "a string", "", true, 5, "another string", false, nil]
arr - [ nil ] # [0, "a string", "", true, 5, "another string", false]
arr - [ false ] # [0, "a string", "", true, 5, "another string", nil]
arr - [ nil, false ] # [0, "a string", "", true, 5, "another string"]
def sort_array_by(arr, key)
arr.sort_by{ |hash| -(hash[key] || 0) }.reverse
end
arr = [
{ name: 'Foo', age: 42 },
{ name: 'Bar', age: 24 },
{ name: 'Fuzz', age: 36 },
{ name: 'Baz', age: 32 },
];
sort_array_by(arr, 'age')
# [{:name=>"Baz", :age=>32}, {:name=>"Fuzz", :age=>36}, {:name=>"Bar", :age=>24}, {:name=>"Foo", :age=>42}]
arr.shuffle
arr1 = [1, 2, 3, 4, 5]
arr2 = ["a", "b", "c", "d", "e"]
arr1.shuffle # ["a", "b", "c", "d", "e"]
arr2.shuffle # ["e", "a", "b", "c", "d"]
matrix.transpose
matrix = [
[1,2,3],
[1,2,3],
[1,2,3]
]
matrix.transpose # [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
def swap_items(arr, key1, key2)
arr[key1], arr[key2] = arr[key2], arr[key1]
end
arr = [4, 5, 6, 7]
swap_items(arr, 1 ,2)
arr # [4, 6, 5, 7]
def remove_duplicate(arr)
arr.uniq
end
remove_duplicate([1,1,2]) # [1,2]
def get_most_appeared(arr)
arr.max_by {|i| arr.count(i)}
end
pets = [ 'dog', 'dog', 'cat', 'bird', 'chinchilla', 'rat', 'hamster', 'dog', 'rat', 'bird', 'cat', 'giraffe', 'cat', 'bird','dog', 'snake', 'hamster', 'dog' , 'mouse', 'spider', 'dog', 'cat']
get_most_appeared(pets) # 'dog'
def midpoint(point1, point2)
[(point1[0] + point2[0]) / 2, (point1[1] + point2[1]) / 2]
end
midpoint([0, 0], [1, 1]) # [0.5, 0.5]
midpoint([0, 0], [2, 4]) # [1, 2]
def linear_interpolation(y1, y2, mu)
y1 * (1 - mu) + y2 * mu
end
linear_interpolation(0, 1, 0.5) # 0.5
linear_interpolation(0, 1, 0.25) # 0.25
def is_point_in_rectangle(point, rectangle)
point[0] >= rectangle[0] && point[0] <= rectangle[2] && point[1] >= rectangle[1] && point[1] <= rectangle[3]
end
is_point_in_rectangle([1, 1], [0, 0, 2, 3]) # true
is_point_in_rectangle([1, 1], [0, 0, 0.5, 0.5]) # false
def angle(point1, point2)
Math.atan2(point2[1] - point1[1], point2[0] - point1[0]) * 180 / Math::PI
end
angle([0, 0], [1, 1]) # 45
angle([0, 0], [1, 0]) # 0
def is_rectangle_contained(rectangle1, rectangle2)
rectangle1[0] <= rectangle2[0] && rectangle1[1] <= rectangle2[1] && rectangle1[2] >= rectangle2[2] && rectangle1[3] >= rectangle2[3]
end
is_rectangle_contained([0, 0, 2, 2], [1, 1, 1.5, 1.5]) # true
is_rectangle_contained([0, 0, 2, 2], [1, 1, 3, 3]) # false
def distance(point1, point2)
Math.hypot(point2[0] - point1[0], point2[1] - point1[1])
end
distance([0, 0], [1, 1]) # 1.4142135623730951
distance([0, 0], [5, 0]) # 5
def degrees_to_radians(degrees)
degrees * Math::PI / 180
end
degrees_to_radians(180) # 3.141592653589793
degrees_to_radians(90) # 1.5707963267948966
def is_rectangle_overlapping(rectangle1, rectangle2)
rectangle1[0] < rectangle2[2] && rectangle1[2] > rectangle2[0] && rectangle1[1] < rectangle2[3] && rectangle1[3] > rectangle2[1]
end
is_rectangle_overlapping([0, 0, 2, 2], [1, 1, 3, 3]) # true
is_rectangle_overlapping([0, 0, 1, 1], [2, 2, 3, 3]) # false
def normalize_ratio(num, min, max)
(num - min) / (max - min)
end
normalize_ratio(0.5, 0, 1) # 0.5
normalize_ratio(5, 0, 10) # 0.5
def radians_to_degrees(radians)
radians * 180 / Math::PI
end
radians_to_degrees(3.141592653589793) # 180
radians_to_degrees(1.5707963267948966) # 90
def round_to_multiple(num, multiple)
(num / multiple).round * multiple
end
round_to_multiple(13, 5) # 15
round_to_multiple(12, 5) # 10
fibo = Hash.new{ |h,k| h[k] = k < 2 ? k : h[k-1] + h[k-2] }
fibo[1] # 1
fibo[2] # 1
fibo[3] # 2
fibo[4] # 3
fibo[5] # 5
fibo[6] # 8
def add_ordinal(num)
num.to_s + %w{th st nd rd th th th th th th}[num % 10]
end
add_ordinal(2) # '2nd'
add_ordinal(11) # '11tst'
def average(*args)
args.reduce(:+) / args.length.to_f
end
average(1, 2, 3, 4) # 2.5
def division(*args)
args.reduce(:/).to_f
end
division(1, 2, 3, 4) # 0.0
def mod(a, b)
((a % b) + b) % b
end
mod(-1, 5) # 4
mod(3, 5) # 3
mod(6, 5) # 1
def remainder(*args)
args.reduce(:%)
end
remainder(1, 2, 3, 4) # 1
def clamp(val, min = 0, max = 1)
[min, [val, max].min].max
end
clamp(199, 10, 25) # 25
def gcd(a, b)
b == 0 ? a : gcd(b, a % b)
end
gcd(10, 15) # 5
def factorial(n)
n <= 1 ? 1 : n * factorial(n - 1)
end
factorial(2) # 2
factorial(3) # 6
factorial(4) # 24
factorial(5) # 120
factorial(6) # 720
def sum(*args)
args.reduce(:+)
end
sum(1, 2, 3, 4) # 10
str.chr
99.chr # "c"
"2".to_i # 2
def decimal_to_binary(num)
num == 0 ? "" : decimal_to_binary(num/2) + num.divmod(2)[1].to_s
end
decimal_to_binary(10) # 1010
def digit_array(num)
num.to_s.chars.map(&:to_i)
end
digit_array(123) # [1,2,3]
def prefix_with_zeros(num, size)
num.to_s.rjust(size, '0')
end
prefix_with_zeros(42, 5) # 00042
def multiply(*args)
args.reduce(:*)
end
multiply(1, 2, 3, 4) # 24
def round_to_digits(num, digits)
num.round(digits)
end
round_to_digits(1.234567, 3) # 1.235
def subtract(*args)
args.reduce(:-)
end
subtract(1, 2, 3, 4) # -8
def truncate_at_decimal(num)
num.to_i
end
truncate_at_decimal(25.198726354) # 25
def truncate_without_rounding(num, digits)
num.truncate(digits)
end
truncate_without_rounding(25.198726354, 1) # 25.1
<condition> ? "true" : "false"
def capitalize(string)
string.capitalize
end
capitalize('fooBar') # 'FooBar'
def is_relative_path(path)
!path.start_with?('/')
end
is_relative_path('foo/bar') # true
is_relative_path('/foo/bar') # false
def is_palindrome(string)
string == string.reverse
end
is_palindrome('tacocat') # true
is_palindrome('awesome') # false
def is_absolute_url(url)
url.start_with?('http://', 'https://')
end
is_absolute_url('https://google.com') # true
is_absolute_url('google.com') # false
def is_anagram(string1, string2)
string1.downcase.chars.sort == string2.downcase.chars.sort
end
is_anagram('iceman', 'cinema') # true
is_anagram('foo', 'bar') # false
def base64_to_uint8_array(base64)
Base64.decode64(base64).bytes
end
base64_to_uint8_array('Zm9vYmFy') # [102, 111, 111, 98, 97, 114]
def is_repeated_character_sequence(string)
string.chars.chunk(&:itself).map(&:last).all? { |x| x.length == 1 }
end
is_repeated_character_sequence('aaaa') # true
is_repeated_character_sequence('aaab') # false
def to_camel_case(string)
string.split('_').map(&:capitalize).join
end
to_camel_case('foo_bar') # 'FooBar'
def letter_to_emoji(letter)
letter = letter.downcase
(letter.ord + 127365).chr(Encoding::UTF_8)
end
letter_to_emoji('a') # '🇦'
letter_to_emoji('b') # '🇧'
def to_pascal_case(string)
string.split('_').map(&:capitalize).join
end
to_pascal_case('foo_bar') # 'FooBar'
def to_slug(string)
string.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
end
to_slug('Hello World!') # 'hello-world'
def windows_to_unix_path(path)
path.gsub('\\', '/')
end
windows_to_unix_path('foo\\bar') # 'foo/bar'
def uint8_array_to_base64(array)
Base64.strict_encode64(array.pack('C*'))
end
uint8_array_to_base64([102, 111, 111, 98, 97, 114]) # 'Zm9vYmFy'
def to_kebab_case(string)
string.gsub(/([a-z])([A-Z])/, '\1-\2').downcase
end
to_kebab_case('fooBar') # 'foo-bar'
to_kebab_case('foo-bar') # 'fooBar'
def snake_to_camel_case(string)
string.split('_').map { |x| x.capitalize }.join
end
snake_to_camel_case('foo_bar') # 'FooBar'
def excel_column_to_number(column)
column.upcase.each_char.map { |x| x.ord - 64 }.join.to_i
end
excel_column_to_number('A') # 1
excel_column_to_number('AA') # 27
def count_words(string)
string.split(' ').length
end
count_words('I love Ruby!') # 3
def count_character(string, character)
string.count(character)
end
count_character('I love Ruby!', 'o') # 1
def decapitalize(string)
string[0].downcase + string[1..-1]
end
decapitalize('FooBar') # 'fooBar'
def get_base_url(url)
url.split('?')[0]
end
get_base_url('https://google.com?q=foo') # 'https://google.com'
def format_string(string, *args)
string % args
end
format_string('I love %s!', 'Ruby') # 'I love Ruby!'
format_string('I love %s and %s!', 'Ruby', 'Python') # 'I love Ruby and Python!'
def get_file_extension(file)
file.split('.').last
end
get_file_extension('foo.rb') # 'rb'
def get_file_name(url)
url.split('/').last
end
get_file_name('https://google.com/foo/bar.jpg') # 'bar.jpg'
def get_string_length(string)
string.bytesize
end
get_string_length('I love Ruby!') # 12
def get_character_number(string, character)
string.chars.count(character)
end
get_character_number('I love Ruby!', 'o') # 1
def lowercase_first_character(string)
string[0].downcase + string[1..-1]
end
lowercase_first_character('FooBar') # 'fooBar'
def normalize_slashes(path)
path.gsub('\\', '/')
end
normalize_slashes('foo\\bar') # 'foo/bar'
def prepend_line_number(file)
File.readlines(file).map.with_index { |x, i| "#{i + 1} #{x}" }.join
end
def remove_duplicate_lines(file)
File.readlines(file).uniq.join
end
def remove_spaces(string)
string.gsub(' ', '')
end
remove_spaces('I love Ruby!') # 'IloveRuby!'
def remove_empty_lines(file)
File.readlines(file).reject { |x| x.strip.empty? }.join
end
def repeat_string(string, count)
string * count
end
repeat_string('I love Ruby!', 3) # 'I love Ruby!I love Ruby!I love Ruby!'
repeat_string('I love Ruby!', 0) # ''
def replace_line_breaks(string)
string.gsub('
', '<br>')
end
replace_line_breaks('I love
Ruby!') # 'I love<br>Ruby!'
def replace_multiple_spaces(string)
string.gsub(/\s+/, ' ')
end
replace_multiple_spaces('I love Ruby!') # 'I love Ruby
def replace_characters(string, character, count)
string.gsub(character, '*', count)
end
replace_characters('I love Ruby!', 'o', 1) # 'I l*ve Ruby!'
replace_characters('I love Ruby!', 'o', 2) # 'I l*v* Ruby!'
def replace_tabs(string, count)
string.gsub("\t", ' ' * count)
end
replace_tabs("I\tlove\tRuby!", 2) # 'I love Ruby!'
def reverse_lines(file)
File.readlines(file).reverse.join
end
def reverse_string(string)
string.reverse
end
reverse_string('I love Ruby!') # !ybuR evol I
def sort_lines(file)
File.readlines(file).sort.join
end
def sort_characters(string)
string.chars.sort.join
end
sort_characters('I love Ruby!') # ' !IbRUVYdeloru'
def swap_case(string)
string.swapcase
end
swap_case('I love Ruby!') # 'i LOVE rUBY!'
def trim_slashes(string)
string.gsub(/^\/|\/$/, '')
end
trim_slashes('/foo/bar/') # 'foo/bar'
def trim_character(string, character)
string.gsub(character, '')
end
trim_character('I love Ruby!', '!') # 'I love Ruby'
def strip_ansi(string)
string.gsub(/\e\[[\d;]+m/, '')
end
strip_ansi("\e[31mI love Ruby!\e[0m") # 'I love Ruby!'
def truncate_string(string, length)
string.length > length ? string[0..length].split(' ')[0..-2].join(' ') + '...' : string
end
truncate_string('I love Ruby!', 5) # 'I...'
truncate_string('I love Ruby!', 10) # 'I love...'
def trim_file_extension(file)
file.split('.').first
end
trim_file_extension('foo.rb') # 'foo'
def uppercase_first_character(string)
string.split(' ').map { |x| x.capitalize }.join(' ')
end
uppercase_first_character('I love Ruby!') # 'I Love Ruby!'
a, b, c = 'a', 123, [1,2,3,4,5]
a # 'a'
b # 123
c # [1,2,3,4,5]
a, b = b, a
a = 5
b = 10
a # 10
b # 5
def random_boolean
[true, false].sample
end
random_boolean #=> true
random_boolean #=> false
def random_integer(min, max)
rand(min..max)
end
random_integer(1, 10) #=> 5
random_integer(1, 10) #=> 8
def random_float(min, max)
rand(min..max)
end
random_float(1, 10) #=> 5.123
random_float(1, 10) #=> 8.123
def random_hex_color
"#%06x" % (rand * 0xffffff)
end
random_hex_color #=> "#e6e6e6"
random_hex_color #=> "#e6e6e6"
def random_string(length, characters)
characters = characters.split('') unless characters.is_a? Array
Array.new(length) { characters.sample }.join
end
random_string(10, 'abc') #=> "bcbabababa"
random_string(10, 'abc') #=> "ababababab"
def random_ip_address
Array.new(4) { rand(256) }.join('.')
end
random_ip_address # "169.18.119.188"
random_ip_address # "126.122.102.185"
random_ip_address # "233.85.50.36"
def random_integers(length, min, max)
Array.new(length) { rand(min..max) }
end
random_integers(10, 1, 10) #=> [5, 8, 1, 2, 5, 1, 2, 5, 1, 2]
random_integers(10, 1, 10) #=> [5, 8, 1, 2, 5, 1, 2, 5, 1, 2]
def random_sign
[-1, 1].sample
end
random_sign #=> -1
random_sign #=> 1
def random_item!(array)
array.delete_at(rand(array.length))
end
array = [1, 2, 3, 4, 5]
random_item!(array) #=> 3
array #=> [1, 2, 4, 5]
def random_string(length)
Array.new(length) { ('a'..'z').to_a.sample }.join
end
random_string(10) #=> "tfdhfuklbz"
random_string(10) #=> "cyiezmppoq"
def random_items(array, count)
array.sample(count)
end
array = [1, 2, 3, 4, 5]
random_items(array, 2) #=> [2, 5]
random_items(array, 2) #=> [1, 4]
random_items(array, 2) #=> [3, 5]
def random_item(array)
array.sample
end
array = [1, 2, 3, 4, 5]
random_item(array) #=> 2
random_item(array) #=> 5
def random_property(object)
object.send
(object.methods.sample)
end
random_property('abc') #=> "a"
random_property('abc') #=> "b"
def random_lines(file, count)
File.readlines(file).sample(count)
end
randomLines(
`one
two
three
four
five`,
2
)
# ['one', 'four']
Submit your PR with your favorite line of code.
Please, add the code snippet to the corresponding category.
Don't forget to always show an example of how your one-liner code works!
Inspired by: https://github.com/phuocng/1loc