[Solved] Is iteration slower than linear code? Which one is preferable?

Sequential logic is faster (see the benchmark below the fold), but it almost never matters. The clearer and more maintainable code should always be selected. Only a demonstrated need should cause one to cease optimizing for the programmer and begin optimizing for the machine. By demonstrated, I mean measured–you ran it and found it to … Read more

[Solved] How to recursively check if a string is palindrome in Ruby [closed]

Smells like homework assignment since you specifically request a recursive solution. A solution with recursion is to check whether first equals last letter and if they are recursively check the string in between. An empty or one character string is a palindrome. def palindrome?(str) str.length <= 1 or (str[0,1] == str[-1,1] and palindrome?(str[1..-2])) end solved … Read more

[Solved] Ruby .each fails for single element

You got that exception because the class String has no instance method each: String.instance_methods.include?(:each) #=> false If packages is a string need to operate on an array comprised of that string alone. We can use the method Kernel#Array to write: Array(packages).each do |package| Array(packages) will return packages if packages is an array and will return … Read more

[Solved] Count IP addresses

If you need to convert an IP to a range, you’ll need a function that converts an IPv4 value to an integer, then do math on those: require ‘ipaddr’ def ip(string) IPAddr.new(string).to_i end def parse_ip(string) string.split(/\s+\-\s+/).collect { |v| ip(v) } end def ip_range(string) ips = parse_ip(string) ips.last – ips.first + 1 end ip_range(“10.2.3.10 – 10.2.3.15”) … Read more

[Solved] Multisort using Ruby [closed]

What you have works. Just don’t forget to assign the sorted collection to a variable (sort_by and reverse do not change the collection). Bonus: here’s arguably a nicer version (one pass, instead of two) a.sort_by{ |v| -v[:a_value] } solved Multisort using Ruby [closed]

[Solved] Ruby regex method

Use regex /r\d+=\d+/: “http://test.com?t&r12=1&r122=1&r1=1&r124=1”.scan(/r\d+=\d+/) # => [“r12=1”, “r122=1”, “r1=1”, “r124=1”] “http://test.com?t&r12=1&r124=1”.scan(/r\d+=\d+/) # => [“r12=1”, “r124=1”] You can use join to get a string output. Here: “http://test.com?t&r12=1&r122=1&r1=1&r124=1”.scan(/r\d+=\d+/).join(‘,’) # => “r12=1,r122=1,r1=1,r124=1” Update If the URL contains other parameters that may include r in end, the regex can be made stricter: a = [] “http://test.com?r1=2&r12=1&r122=1&r1=1&r124=1&ar1=2&tr2=3&xy4=5”.scan(/(&|\?)(r+\d+=\d+)/) {|x,y| a << … Read more

[Solved] return value from loop

To return a value from a loop, use break value: def my_def(port) @hsh.each do |k, v| break k if v == port end end In general, @Stefan’s comment solves this particular problem better: def my_def port @hsh.key port end solved return value from loop

[Solved] Undefined method ‘[]’ for nil:NilClass when trying to write to db in active record [closed]

I figured out the reason for ArgumentError: uncaught throw “Error in record 314675: undefined method ‘[]’ for nil:NilClass’ was primarily from an incomplete set of input variables that is required by the def init method. Because this was an ArgumentError that was thrown from the function1 method, I was only invested in debugging function1. The … Read more

[Solved] Ruby undefined method ‘each’ for class

Which makes absolute sense because that class indeed does not contain that method, but as I’ve been reading should ruby search the classes parents and grandparents for the method? That’s correct, but you didn’t declare any superclasses so the superclass will be Object. Which also doesn’t have an each method. If you want an enumerable … Read more