I have a few files:
Fastfile with contents like this
platform iOS: do
lane :my_lane do
...
end
def my_func? do
return some_bool
end
end
And my_file.rb with
platform :iOS do
lane :my_other_lane do
...
end
class MyClass
def my_class_func
if my_func? # <--- there is the problem
end
end
def my_non_class_func
if my_func? # <--- It's OK
end
end
I want to call my_func? in class func but error occurs: NoMethodError: [!] undefined method `my_func?' for #Fastlane::FastFile::MyClass:0x0000some_address
What namespace should it have?
Related
I have a ruby module like this:
module SomeModule
module Account
def self.account_info
raise NotImplementedError
end
end
end
and this is my test:
describe ExchangeClientWrapper::Account do
let(:mock_class) do
class MockClass
extend SomeModule::Account
end
end
describe ".account_info" do
it "raises a NotImplementedError" do
expect { mock_class.account_info }.to raise_error(NotImplementedError)
end
end
end
end
I get this error:
expected NotImplementedError, got #<NoMethodError: undefined method `account_info' for MockClass:Class> with backtrace:
What is going on?
Does this not work:
module A
module B
def self.d
puts "hi there"
end
end
end
class C
extend A::B
end
C.d
But it appears this works:
module A
module B
def self.d
puts "hi there"
end
end
end
class C
include A
end
C::B.d
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
I have a mixin (Api::Utility) that is referenced by two classes: Api::Utility::One::WorkstationClient and Api::Utility::Two::WorkstationClient. Both classes define a constant, BASE_URL, that I'd like to access in the mixin. When I attempt to access the constant, I get an error that reads `NameError (uninitialized constant Epic::Api::Utility::BASE_URL).
#!/usr/bin/env ruby
module Api
class WorkstationClientFactory
def self.create_client(version = :v2)
case version
when :v2
Two::WorkstationClient.new()
when :v1
One::WorkstationClient.new()
else
raise ArgumentError, "'#{version.inspect}' is not a valid version symbol. Valid: [:v1|:v2]."
end
end
end # /WorkstationClientFactory
# Api::Utility
module Utility
def bar
puts "BASE_URL: #{BASE_URL}"
end
end
module One
# Api::One::WorkstationClient
class WorkstationClient
include Api::Utility
BASE_URL = 'https://domain.tld/api/v1'
def initialize()
puts "Creating #{self.class.name}"
end
end # /WorkstationClient
end # One
module Two
# Api::Two::WorkstationClient
class WorkstationClient
include Api::Utility
BASE_URL = 'https://domain.tld/api/v2'
def initialize()
puts "Creating #{self.class.name}"
end
end # /WorkstationClient
end # /Two
end # /module
v1 = Api::WorkstationClientFactory.create_client(:v1)
v1.bar <-- error here
BASE_URL: https://domain.tld/api/v1 <-- desired result
v2 = Api::WorkstationClientFactory.create_client(:v2)
v2.bar <-- error here
BASE_URL: https://domain.tld/api/v2 <-- desired result
What's the right way to access the BASE_URL in the mixin, in this situation?
You can use self.class::BASE_URL.
Under Ruby 2.0, what is the correct way to access a module method from a class in that module?
For instance, if I have
module Foo
class Foo
def do_something
Foo::module_method
end
end
def self.module_method
puts 'How do I call this?'
end
end
I get,
./so-module.rb:7:in do_something': undefined methodmodule_method'
for Foo::Foo:Class (NoMethodError) from ./so-module.rb:16:in `'
What is the correct way to define the module method so I can access it from class Foo?
You have to define the method on the module’s singleton class:
module Foo
class Bar
def do_something
Foo.module_method
end
end
def self.module_method
'Success!'
end
end
Foo::Bar.new.do_something #=> "Success!"
Please take a look at following code:
module Foo
class Bar
include Foo
def do_something
module_method
end
end
def module_method
puts 'How do I call this?'
end
end
b = Foo::Bar.new
b.do_something
result:
How do I call this?
You can even try adding following code:
b1 = Foo::Bar::Bar.new
b1.do_something
It's tricky and has same result:
How do I call this?
I must be making a n00b mistake here. I've written the following Ruby code:
module Foo
def bar(number)
return number.to_s()
end
end
puts Foo.bar(1)
test.rb:6:in <main>': undefined methodbar' for Foo:Module (NoMethodError)
I wish to define a single method on a module called Foo.bar. However, when I try to run the code, I get an undefined method error. What am I doing wrong?
You could do with:
module Foo
def self.bar(number)
number.to_s
end
end
puts Foo.bar(1)
Every module in Ruby can be mixed in an object. Once a class is an object, you could mix the methods in a class using the word extend:
module Foo
def bar
'bar'
end
end
class MyInstanceMethods
include Foo
end
class MyClassMethods
extend Foo
end
## Usage:
MyInstanceMethods.new.bar
=> "bar"
MyClassMethods.bar
=> "bar"
If you wish calling the bar method directly from the Foo module, you could do in the same way #xdazz wrote, but since the extend word mixes to a Class:
MyInstanceMethods.class
=> Class
MyClassMethods.class
=> Class
Module.class
=> Class # Hey, module is also a class!!!!!
The trick:
module Foo
extend self # self of Foo is the Module!
def bar
# .....
end
end
Now you can see Foo.bar returning the expected result :P
I'm trying to override a method from another gem. The code looks something like this:
module DatabaseCleaner
class Base
def orm_strategy(strategy)
# ...
end
end
end
In my gem:
require 'database_cleaner/base'
module DatabaseCleaner
class Base
def orm_strategy(strategy)
# New code
end
end
end
However, it the original is still being used. What am I doing wrong?
You are trying to override an instance method of the Foo::Bar class. You have to redefine the class's method:
module Foo
class Bar
def self.test # self == Bar
# New code
end
end
end