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 |
Add a space function in Ruby π
1 2 3 4 5 6 7 |
def space(n) str = '' n.times do str += ' ' end str end |
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 |
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 |
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 |
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 |
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 |
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 |
Calculate how many days in month by index in Ruby
1 2 3 |
def days(index) ((15662003>>(2*(index-1)))&3) + 28 end |