Tag ruby

[Solved] What is za and what is next

String#tr allows c1-c2 as a shorthand notation for a range of characters: a-z expands to abcdefghijklmnopqrstuvwxyz b-z expands to bcdefghijklmnopqrstuvwxyz b-za is b-z followed by a single a, i.e. bcdefghijklmnopqrstuvwxyza Finally s.tr!(‘a-z’, ‘b-za’) replaces each letter by the next one…

[Solved] Compare keys of array in RUBY

I corrected your code also as per your need, and solved further, $ArrayX = [8349310431,8349314513] $ArrayY = [667984788,667987788] $ArrayZ = [148507632380,153294624079] $range_map = $ArrayX.zip([$ArrayY.map(&:to_i), $ArrayZ.map(&:to_i)].transpose).sort $ArrayX = [8349310431,8349314513] => [8349310431, 8349314513] $ArrayY = [667984788,667987788] => [667984788, 667987788] $ArrayZ = [148507632380,153294624079]…

[Solved] How to find out the number of records in a table? [closed]

It would be something like this on your model: def self.pending_count where(status: 0).count end def self.in_progress_count where(status: 1).count end def self.finished_count where(status: 2).count end Then you can just call Model.pending_count (or the other methods) wherever you need it. solved How…

[Solved] Regex for price with   and comma [closed]

Here’s how I’d do it: str = “8 560,90 cur.” str.gsub(/[^\d,]/, ”).to_i # => 8560 This removes every character that isn’t a digit or a comma, yielding “8560,90”, then calls to_i on it, which gives 8560. This will work for any…

[Solved] How to parse time given an hours [closed]

Parse the hour with Time#strptime hour = 4 Time.strptime(“#{hour}”, “%H”) => 2015-11-02 04:00:00 +0000 And then parse with Time#strftime if you’re interested in the formatting. Time.strptime(“#{hour}”, “%H”).strftime(“%H:%M”) => “04:00” solved How to parse time given an hours [closed]

[Solved] Initialize Ruby codes error

class Dog def set_name(name) @dogname = name end def get_name return @dogname end def talk return “awww” end def initialize(title, description) @title = title @description = description end end #That will cause an error because your new method have two…

[Solved] Rails: Remove Curly Braces in Array [closed]

@temp = SalesOrder.where(“status > ?”, 0).ids items = SalesOrderItem.where(sales_order_id: @temp).where.not(product_id: nil) total = items.to_a.group_by(&:product_id).each_with_object({}) do |(product_id, quantity), total| total[Product.find(product_id.to_i).name] = quantity.map(&:quantity).map(&:to_f).sum end @top_five = total.sort_by { |k, v| v }.reverse! Check this. It should work. If any errors, ping me,…