Ruby

Member

makayla.beahan

by makayla.beahan , in category: Ruby , 24 days ago
no answers

Progress Bar on the Ruby 😝

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# progress bar

width = 60  # width of bar
com   = 540 # input data

pr = com * 0.01
i = 0
j = width

v = 1
puts
while pr <= com

  print "\r#{v}% [#{"|"*i}#{" "*j}]"

  pr += com * 0.01  

  i += width * 0.01
  j -= width * 0.01

  v += 1  
  
  sleep(0.01)
  
end
puts


Member

letha

by letha , in category: Ruby , 24 days ago
no answers

Add a space function in Ruby πŸ˜ƒ

1
2
3
4
5
6
7
def space(n)
    str = ''
    n.times do
      str += '&nbsp;'
    end
    str
  end


Member

carey

by carey , in category: Ruby , a month ago
no answers

Sum All Order function in Ruby

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def sum_all_orders
    sum = 0
    Order.where(user_id: self.id).each do |order|
      sum = sum + order.total_sum
    end
    sum
  end

  # вмСсто
  def sum_all_orders
     orders.sum(&:total_sum)
  end


Member

lou_lindgren

by lou_lindgren , in category: Ruby , 2 months ago
no answers

Catch exceptions in Ruby 😝

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
begin
    # etc
  rescue Exception => e
    case e
      when LinkedIn::Unauthorized
        account.invalidate_token if !account.invalid_token?
        raise InvalidTokenException.new(account.primary, provider_name)
      when LinkedIn::InformLinkedIn, LinkedIn::Unavailable  #LinkedIn::Unavailable represents 502..503 error codes & LinkedIn::InformLinkedIn represent 500
        raise UnexpectedApiException.new(provider_name)
      else
        handle_api_exception(e, e.message)
    end
  end


Member

jerald_rippin

by jerald_rippin , in category: Ruby , 2 months ago
no answers

Funny way to check role in Ruby πŸ˜—

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def type
    return 'Anonymous'   if self.builtin == BUILTIN_ANONYMOUS
    return 'Non Member'  if self.builtin == BUILTIN_NON_MEMBER
    return 'Member'      if self.builtin == BUILTIN_MEMBER
    return 'User'        if self.builtin == BUILTIN_USER
    return 'Manager'     if self.builtin == BUILTIN_MANAGER
    return 'Architect'   if self.builtin == BUILTIN_ARCHITECT
    return 'Designer'    if self.builtin == BUILTIN_DESIGNER
    return 'Customer'    if self.builtin == BUILTIN_CUSTOMER
    return 'Vendor'      if self.builtin == BUILTIN_VENDOR
    return 'Dealer'      if self.builtin == BUILTIN_DEALER
  end

  def require_name
    return 'is_anonymous'   if self.builtin == BUILTIN_ANONYMOUS
    return 'is_non_member'  if self.builtin == BUILTIN_NON_MEMBER
    return 'is_member'      if self.builtin == BUILTIN_MEMBER
    return 'is_user'        if self.builtin == BUILTIN_USER
    return 'is_manager'     if self.builtin == BUILTIN_MANAGER
    return 'is_architect'   if self.builtin == BUILTIN_ARCHITECT
    return 'is_designer'    if self.builtin == BUILTIN_DESIGNER
    return 'is_customer'    if self.builtin == BUILTIN_CUSTOMER
    return 'is_vendor'      if self.builtin == BUILTIN_VENDOR
    return 'is_dealer'      if self.builtin == BUILTIN_DEALER
  end


Member

adelia_boyer

by adelia_boyer , in category: Ruby , 2 months ago
no answers

Parse Path URL in Ruby 😁

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def set_path
      if name && (!path || path == "/")
        self.path = self.parent.present? ? "#{self.parent.path}/#{name}" : "/#{name}"
      elsif !new_record? && name && path && name_was != name
        parts = path.split("/")
        parts.pop
        self.path = [parts.join("/"), name].join("/")
      elsif !new_record? && name && self.parent_id_changed?
        self.path = self.parent.present? ? "#{self.parent.path}/#{name}" : "/#{name}"
      elsif new_record? && name && path
        self.path = [path, name].join("/")
      end

      if path && self.parent.blank?
        parts = path.split("/")

        self.name = parts.pop

        parent_path = parts.join("/")
        if parent_path.blank? || parent_path == "/"
          self.parent = nil
        else
          possible_parent = site.asset_folders.find_by_path(parent_path)
          self.parent = possible_parent.present? ? possible_parent : self.class.create(path: parent_path, site: site)
        end
      end
      true
     end


Member

adelia_boyer

by adelia_boyer , in category: Ruby , 2 months ago
no answers

Add Zeroes Function in Ruby 😁

1
2
3
4
5
6
def self.add_zeroes(string, stringSize, leftOrRight = true)
  while string.size < stringSize
    string = leftOrRight ? "0" + string : string + "0"
  end
  string
end


Member

vivien_maggio

by vivien_maggio , in category: Ruby , 2 months ago
no answers

Check in page has any children in Ruby

1
2
3
4
5
6
def has_children_pages(id)
  unless Page.where(:parent_id => id, :public => true).blank?      
    return true
  end      
  return false
end


Member

lou_lindgren

Calculate how many days in month by index in Ruby

1
2
3
def days(index)
  ((15662003>>(2*(index-1)))&3) + 28
end