How to Access a Toplevel Entity in Ruby, from Inside a Module Which Defines The Same Name

How to access a toplevel entity in ruby, from inside a module which defines the same name

::Process.wait(0)

@object = ::tablename.where ( ) in ruby

The snippet checks to see if @object is false or nil; if it is, it assigns it the value produced by calling the method named Tablename in the top-level namespace.

This is because ||= means "assign if the value is currently falsey"; and :: is the scope resolution operator. Often it appears with a class name on the left, as in Net::HTTP accessing the HTTP constant in the Net module. Without a name on the left, it means "in the top-level scope".

How to require a class when building a ruby gem?

In your code:

module MyApp::UseCases::Entity1
class ShowEntity < BaseUseCase
end
end

Ruby will first look for BaseUseCase in the current module, MyApp::UseCases::Entity1. Assuming this class is defined under MyApp::UseCases to match your file structure, it won’t be found there. Ruby then looks in the enclosing scope to try to find the class. However, since you’ve used the double colon syntax, the enclosing scope is the top–level, not MyApp::UseCases as you may be expecting. Ruby “jumps over” all the namespaces in the statement module MyApp::UseCases::Entity1.

If instead you defined the class like this it should work:

module MyApp::UseCases
module Entity1
class ShowEntity < BaseUseCase
end
end
end

Here, when BaseUseCase isn’t found in the current scope the enclosing scope searched is MyApp::UseCases. Ruby steps back a single module statement at a time, and since there is now a separate declaration for Entity1, the next scope is MyApp::UseCases instead of the top level. If BaseUseCase were defined directly under MyApp instead then this code wouldn’t work either, since after failing to find it under MyApp::UseCases::Entity1 and MyApp::UseCases the next scope searched would be the top level.

To fix it you should break out your module declarations as needed as above, or explicitly define the fully namespaced name of BaseUseCase:

class ShowEntity < ::MyApp::UseCases::BaseUseCase

How to avoid Rspec shared examples 'previously defined' warning?

I found the answer in this issue at the Rspec Github:

Just in case someone googles and lands here. If putting your file with
shared examples into support folder has not fixed the following
error...Make sure your filename does not end with _spec.rb.

How to list all methods for an object in Ruby?

The following will list the methods that the User class has that the base Object class does not have...

>> User.methods - Object.methods
=> ["field_types", "maximum", "create!", "active_connections", "to_dropdown",
"content_columns", "su_pw?", "default_timezone", "encode_quoted_value",
"reloadable?", "update", "reset_sequence_name", "default_timezone=",
"validate_find_options", "find_on_conditions_without_deprecation",
"validates_size_of", "execute_simple_calculation", "attr_protected",
"reflections", "table_name_prefix", ...

Note that methods is a method for Classes and for Class instances.

Here's the methods that my User class has that are not in the ActiveRecord base class:

>> User.methods - ActiveRecord::Base.methods
=> ["field_types", "su_pw?", "set_login_attr", "create_user_and_conf_user",
"original_table_name", "field_type", "authenticate", "set_default_order",
"id_name?", "id_name_column", "original_locking_column", "default_order",
"subclass_associations", ...
# I ran the statements in the console.

Note that the methods created as a result of the (many) has_many relationships defined in the User class are not in the results of the methods call.

Added Note that :has_many does not add methods directly. Instead, the ActiveRecord machinery uses the Ruby method_missing and responds_to techniques to handle method calls on the fly. As a result, the methods are not listed in the methods method result.

ActiveResource post raises an error, while doing the post in my browser succeeds

It sounds like the wrap_parameters code is confused by the fact that your model is namespaced inside the Mongo module.

It also sounds like you don't actually need parameter wrapping so the easiest thing is to disable it - there should be a wrap_parameters initializer. You can re-enable it on a controller by controller basis if you need to.

What the difference between a namespace and a module in F#?

A namespace is a .Net thing, common in many industrial-strength languages, just a way to organize frameworks and avoid naming conflicts among different libraries. Both you and I can define a type "Foo" and use them both in a project, provided they are in different namespaces (e.g. NS1.Foo and NS2.Foo). Namespaces in .Net contain types.

A module is an F# thing, it is roughly analogous to a "static class"... it is an entity that can hold let-bound values and functions, as well as types (note that namespaces cannot directly contain values/functions, namespaces can only contain types, which themselves can contain values and functions). Things inside a module can be referenced via "ModuleName.Thing", which is the same syntax as for namespaces, but modules in F# can also be 'opened' to allow for unqualified access, e.g.

open ModuleName
...
Thing // rather than ModuleName.Thing

(EDIT: Namespaces can also similarly be opened, but the fact that modules can contain values and functions makes opening a module more 'interesting', in that you can wind up with values and functions, e.g. "cos", being names you can use directly, whereas in other .Net languages you'd typically always have to qualify it, e.g. "Math.cos").

If you type in code at 'the top level' in F#, this code implicitly goes in a module.

How to convert JSON data into a Python object?

You could try this:

class User(object):
def __init__(self, name, username):
self.name = name
self.username = username

import json
j = json.loads(your_json)
u = User(**j)

Just create a new object, and pass the parameters as a map.


You can have a JSON with objects too:

import json
class Address(object):
def __init__(self, street, number):
self.street = street
self.number = number

def __str__(self):
return "{0} {1}".format(self.street, self.number)

class User(object):
def __init__(self, name, address):
self.name = name
self.address = Address(**address)

def __str__(self):
return "{0} ,{1}".format(self.name, self.address)

if __name__ == '__main__':
js = '''{"name":"Cristian", "address":{"street":"Sesame","number":122}}'''
j = json.loads(js)
print(j)
u = User(**j)
print(u)


Related Topics



Leave a reply



Submit