Unexpected Value of _Callee_ When Including a Module - Is This a Ruby Bug

Unexpected value of __callee__ when including a module – is this a Ruby bug?

This was a bug, and it was closed 3 days ago with this note:

Seems fixed by r56592.

Testing a ruby module class method not working

Reading the reference in this article which explains include, extend, and prepend and I found out that extends works with the Singleton class already so the self is unnecesary.

I made a small test with this code, which removes the self in the definition

module SomeModule
module Account
def account_info
raise NotImplementedError
end
end
end

class MockClass
extend SomeModule::Account
end

MockClass.account_info

And that raises NotImplementedError

How do I check if a module responds to a class method?

First of all, you're missing an end for the def initialize block so your create_class method is getting defined in the MyClass body - you should fix the indentation to make these kinds of errors more obvious.

After fixing this, you can use MyModule.respond_to?(:create_class). The reason method_defined? doesn't work as expected with class methods is explained here.

Is there an advantage of using indirection when including a module (from 7 Languages In 7 Weeks, Ruby day 3)

The original code allows you to do this:

class A
include ActsAsCsv
end

class B < A
acts_as_csv
end

While using inheritance is more a matter of taste in this case it would be cleaner if

  • more methods than just acts_as_csv would be added similarly to A
  • ActsAsCsv would be expected to be included by a class from which many classes inherit (like some kind of BaseModel)

Further advantage arise when

  • the method acts_as_csv is more elaborate than just including InstanceMethods
  • acts_as_csv takes an argument, for example acts_as_csv :separator => " "

Access class from inside included module

I don't believe modules keep track of what included them. But they do fire a MyModule.included(SomeClass) method as a callback when they get included, so you can keep track yourself.

module IncludedModule

# Array to store included classes
@@included_classes = []

# Called when this module is included.
# The including class is passed as an argument.
def self.included(base)
@@included_classes << base
end

# Getter for class variable
def self.included_classes
@@included_classes
end
end

# Include the module
class IncludingClass
include IncludedModule
end

# Ask the module what included it.
puts IncludedModule.included_classes #=> [IncludingClass]

There's probably also a way to crawl all Classes declared and ask them what they included via SomeClass.included_modules but that's kind of hairy, and would be much slower.

When Hash's default value is set as Hash, it is giving unexpected output

When you use the default value, no key/value is set. The default is simply returned instead of nil.

I think you're imagining it works like this where the default is set on the key being accessed like ||=.

default = {x: 0, y: 0}
foo = Hash.new
foo['bar'] ||= default
foo['bar'][:x] += 1

Instead, it works like this where the default is returned when there is no key.

default = {x: 0, y: 0}
foo = Hash.new
val = foo['bar'] || default
val[:x] += 1

Put another way, you're expecting this.

def [](key)
@data[key] ||= default
end

But it works like this.

def [](key)
@data[key] || default
end

But this behaviour appears to change if I provide, say, an integer instead of a Hash as the default value. For instance, if I do foo = Hash.new(1), then foo['bar'] += 1 the behaviour is what I would expect. foo is not empty, and the default value has not changed. – aardvarkk 4 mins ago

foo['bar'] += 1 is really shorthand for

default = foo['bar']        # fetch the default
foo['bar'] = default + 1 # sets 'bar' on foo

Note that it calls []= on foo.

foo['bar'][:x] += 1 is shorthand for...

default = foo['bar']   # fetch the default value
val = default[:x] # fetch :x from the default
default[:x] = val + 1 # set :x on the default value

Note that it calls []= on the default value, not foo.

Enumerator#each Restarts Sequence

You're not doing it wrong, that's just not the semantics defined for Enumerator#each. You could make a derivative enumerator that only iterates from current position to end:

class Enumerator
def enum_the_rest
Enumerator.new { |y| loop { y << self.next } }
end
end

o = Object.new
def o.each
yield 1
yield 2
yield 3
end

e = o.to_enum
=> #<Enumerator: ...>
e.next
=> 1
e2 = e.enum_the_rest
=> #<Enumerator: ...>
e2.each { |x| puts x }
=> 2
=> 3

And, BTW, each doesn't restart the sequence, it just always runs over the entire span. Your enumerator still knows where it is in relation to the next next call.

e3 = o.to_enum
e3.next
=> 1
e3.next
=> 2
e3.map(&:to_s)
=> ["1", "2", "3"]
e3.next
=> 3

unable to mock httparty request on service module test

BASE_URI is being set when your source file is loaded, which is before you've mocked ENV to return the value you expect. Therefore it equals nil and is causing your issue.



Related Topics



Leave a reply



Submit