#64 Random Ruby Tips and Tricks

Summary

A collection of different Ruby tips and tricks. Hope you find some of them useful!
ruby tricks tips 10:34

Resources

Summary

# irb
def my_method(variable, *array, **json)
  return variable, array, json
end

my_method(1)
# => [1, [], {}]

my_method(1, 2, 3, 4)
# => [1, [2, 3, 4], {}]

my_method(1, 2, 3, 4, a: 5, b: 6)
# => [1, [2, 3, 4], {:a=>5, :b=>6}]

# irb
stuff = 1
stuff_arr = [1, 2, 3]

[*stuff].each { |s| s }
[*stuff_arr].each { |s| s }

# Careful!

a = { :name => "DK", :age => 30 }
[a]                                  # => [{:name => "DK", :age => 30}]
[*a]                                 # => [[:name, "DK"], [:age, 30]]

# irb
a
# => nil

a ||= 1
# => a = 1

a = a || b # incorrect
a || a = b # correct

string = nil
string &&= string + "suffix"
# string is still nil

string = 'some value '
string &&= string + "suffix"
# => some value suffix

# irb
money = 42.5
"%.2f" % money
# => "42.50"

"%0.2f" % Math::PI  # => "3.14"

# irb
#Dependency Ruby 2.3.0

current_user.try(:full_name)
current_user&.full_name

# irb
def is_odd(x)
  if x % 2 == 0
    return false
  else
    return true
  end
end

def is_odd(x)
  x % 2 == 0 ? false : true
end

def is_odd(x)
  x % 2 != 0
end

# irb
strs = ['foo', 'bar', 'baz']
caps = strs.map { |str| str.upcase }
caps = strs.map(&:upcase)

cars = %w[beetle volt camry]
=> ["beetle", "volt", "camry"]

cars.sort_by { |car| car.size }
=> ["volt", "camry", "beetle"]

cars.sort_by(&:size)
=> ["volt", "camry", "beetle"]

# irb
food = %w( tacos milk eggs )
food.map(&:object_id)       # [x1, y1, z1]
food.clone.map(&:object_id) # [x1, y1, z1]

# Using the Marshal class, which is normally used for serialization, you can create a ‘deep copy’ of an object.

def deep_copy(obj)
  Marshal.load(Marshal.dump(obj))
end

deep_copy(food).map(&:object_id) # [x2, y2, z2]

# irb
Array.new(10)
# => [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
Array.new(10) { rand 100 }
# => [77, 34, 79, 43, 70, 86, 43, 96, 18, 3]

# irb
Impression.select(:id, :impressionable_type, :impressionable_id).last(10)
  Impression Load (0.3ms)  SELECT  `impressions`.`id`, `impressions`.`impressionable_type`, `impressions`.`impressionable_id` FROM `impressions` ORDER BY `impressions`.`id` DESC LIMIT 10
# => [#, #, #, , #, #, , #, #, ]

Impression.select(:id, :impressionable_type, :impressionable_id).last(10);
# =>  Impression Load (0.3ms)  SELECT  `impressions`.`id`, `impressions`.`impressionable_type`, `impressions`.`impressionable_id` FROM `impressions` ORDER BY `impressions`.`id` DESC LIMIT 10

Impression.select(:id, :impressionable_type, :impressionable_id).last(10);
# => Impression Load (0.3ms) SELECT `impressions`.`id`, `impressions`.`impressionable_type`, `impressions`.`impressionable_id` FROM `impressions` ORDER BY `impressions`.`id` DESC LIMIT 10

# irb
items = { :tacos => 12, :eggs => 3 }
# => items = {:tacos=>12, :oranges=>3}

items.fetch(:tacos)
# => 12

items.fetch(:bananas) { |key| "We don't carry #{key}!"}
# => We don't carry bananas!

items.fetch(:eggs)
# => 3

# irb
100.to_s(2)
# => "1100100"

"1100100".to_i(2)
# => 100