Ad
  • Custom User Avatar
  • Custom User Avatar

    I agree that Hash resulting from a proper solution to this kata would be an obscenity in production code, but I would still suggest people try to solve the kata in order to see how well they understand ruby. I tried, failed, and learned some things in the process (My thanks to @ineiti).

  • Custom User Avatar

    For the Ruby version:

    1. Don't use clsses as a way to namespace static methods
    2. Use module_function to define module-level methods, not def self.foo. This plays nicer with both include and extend.

    See my soln: http://www.codewars.com/kata/reviews/54f7910c20de9891c400004b/groups/5545e362647c1f924600007b

  • Custom User Avatar

    This kata needs a lot of work. To those struggling with this kata, it's honestly not worth your time. You're better off reading reading the ostruct.rb file that comes with the default Ruby interpreter to learn how this sort of thing should work.

    1. The instructions are unclear
    2. The goal is unclear — are we trying to make a Hash-like object that is indifferent to whether a Symbol or String is used as a key?
    3. The standard library's OpenStruct class appears to do all this and in a more sensible way (see this screenshot)
    4. It's Bad Ruby Style™ to start method names with an underscore (feels like a Python-ism)
    5. It's a Bad Idea™ to override method_missing without calling super.
    6. It's a Bad Idea™ to create objects that respond to a new method foo without having respond_to?(:foo) return true.
    7. It's a Bad Idea™ to re-open built-in classes and add new methods, let alone override standard methods. What if the built-in Hash class already defined its own method_misssing? This is why libraries like hashie subclass Hash.
    8. Even then, there are good arguments for why libraries like hashie are harmful and almost never what you really want.
  • Custom User Avatar

    I thought the same thing, but apparently starting with 0 is the "modern" form according to Wikipedia: http://en.wikipedia.org/wiki/Fibonacci_number

    That being said Wolfram Alpha calls 0 the 0th Fibonacci. But meh.

  • Custom User Avatar

    And the usage of class variables. I'd say that's the most off.

  • Custom User Avatar

    You might be interested in a solution I posted: http://www.codewars.com/dojo/katas/reviews/52255487906b0c1733000452/groups/522cd249900801524d000063

    It starts by implementing "exact arithmetic" in the field extension ℚ(𝜑) = {a + b𝜑 | a,b ∈ ℚ}, where ℚ are the rational numbers and 𝜑 is the golden ratio. Since

    fib(n) = [𝜑 ⁿ - (1-𝜑) ⁿ]/(2𝜑 - 1)
    

    we can implement the "explicit formula" by implementing an algorithm for exponentiation in ℚ(𝜑) .

    In the code, the representation works as follows

    PhiRational.new(a,b)  # a + b𝜑
    PhiRational.new(0,1) # 𝜑
    # etc.
    
  • Custom User Avatar

    Why? It's not like you have to do every exercise.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Just FYI, in Ruby you can do

    some_array.each(&:foo)
    

    instead of

    some_array.each { |item| item.foo }
    

    This holds for any method which takes a block. The ampersand ("&") tells Ruby that this argument should serve as the block argument to the method and Ruby will then call to_proc on that object. In this case, Ruby will call Symbol#to_proc since you're passing in a symbol, which works thus:

    class Symbol
      def to_proc
        Proc.new { |object| object.send(self) }
      end
    end
    
  • Custom User Avatar

    This code isn't just unidiomatic, it's a little bit abusive. :)

    You define a constant CHILDS and then modify it. Don't modify constants! Ruby lets you do this—don't ask me why—but it will at least throw a warning about the constant already being defined.

    Also, the plural of "child" is "children," which would be a better variable name.

  • Custom User Avatar

    The plural of "child" is "children," which would be the more appropriate variable name. Also, there's no reason to call Array#map here. You should just call Array#each.

  • Custom User Avatar

    There's no reason to call Array#map here. You should just call Array#each.

  • Custom User Avatar

    Idiomatically, there are three things off about your code:

    1. If a method takes a Class as input, one traditionally uses the variable name klass.
    2. Don't put spaces before and after method arguments.
    3. If you have a multi-line block, use do...end, not {...}.
  • Custom User Avatar

    There's no reason to call Array#map here. You should just call Array#each.

  • Loading more items...