Eiffel - How can I initialize a LINKED_LIST? - linked-list

I'm new to Eiffel and I'm trying to create an instance of Linked_List. I'm not really sure of how to do this with this class because I receive an syntax error whenever I try to do it that way. This is what I have:
class
APPLICATION
inherit
ARGUMENTS
create
make
feature {NONE} -- Initialization
make
--
local
lista:LINKED_LIST[MONOMIO]
do
lista.make
end
end
And the error I'm getting is:
Error code: VUEX(2)
Error: feature of qualified call is not available to client class.
What to do: make sure feature after dot is exported to caller.
I hope somebody can help me with this, thanks.

Objects are created with a creation instruction, so in your example you need to add a keyword create in front of lista.make to indicate that this is not a plain feature call:
create lista.make

Related

RIght way of writing module methods in Ruby

what is right way of writing module? is it only used to stock some peace of code to minimize the number of lines, or is it something much more important than that
I have used and seen ways of writing module, I am working on setting up correct way to define and standardised module. this example is kind of controller code that we use in rails
Way 1 :-
module B
extend ActiveSupport::Concern
def process_items
# do somthing...
#items.pluck(:names)
end
end
Class A
include B
def index
#items = Item.all
#item_names = process_items
end
end
Way 2 :-
module B
extend ActiveSupport::Concern
def process_items(items)
# do somthing...
items.pluck(:names)
end
end
Class A
include B
def index
#items = Item.all
#item_names = process_items(#items)
end
end
Way 1 :-
When I see this independently, its not much readable as I don't know how #items appeared in this method
Unit testing would be hard for method as its dependent
Way 2 :-
Looking at method I can see input is coming we are processing it and returning it back (readablity is good)
Unit testing is easy to this, we wll call method pass what it needs and expect
The way I see modules should be independent, self explanatory, it should be generic so that can be used in any class, kind of helpers. But other way could be dependent on where we use modules
We are using modules like in rails
We use conccern in models, when we call module method we can use self.<field> we don't need to pass anything because instance variable is supposed to be accesssable in every instance method
View helpers are modules I see they put logic into it hard to understand how the variable come from may be instance variable or params, what about making it method which accept somthing and return it back
Concerns on controllers, like the example I have given
I would like to have thoughts on this, what is best approach out of it? is it something which can be standarise or it is more situational or I don't know yet :)
Note: -
I was looking at this question but answer given on this question is no more valid as referenced links are not working.
Right Way to Use Module
The difference here is practically academic, as if you have attr_reader :x then both #x and x will have the same meaning.
It's understood that within a mixin module you will be referencing methods and/or variables that are part of the class or module doing the "mixing in". As such, seeing #x, or in your case, #items, should not come as a real surprise.
If you want to add it as an explicit argument you're sort of missing a lot of the benefits of using a mixin in the first place. You don't need to mix it in at all, you can just use it like B.process_items(...). In other words, your second approach is having an identity crisis. Is it a stand-alone module that includes Concern for no reason, or a weak mixin?
When it comes to testing, you must test the mixin in a module or class which implements the required features. In this case you need either an #items variable, or an items method, and that must have a value of the expected type.
This should be documented somewhere for clarity, but is effectively an implicit contract with anyone using this module.

Rails NoMethodError when calling a method from a constant

I'm trying to call a method inside a class constant. It returns a NoMethodError. Here's a sample code:
class TestingA
CONSTANT_HERE = { details: get_details('example.json') }
def get_details(file)
# retrieve details here
end
end
The error that appears is:
NoMethodError (undefined method `get_details' for TestingA:Class)
Any ideas on why?
Generally, dynamic constant assignment is discouraged in Ruby. Why would you want to define a constant that can possibly change within the life-cycle of an object? We don't know exactly what get_details is doing, but what is the use case of creating an instance method that is called from a constant as well as exposing the method? We can only assume return value is dynamic at this stage. Rubocop is also going arrest you for not freezing the constants, which is bad as linters are a good tool to abide by.
Constants can be changed and there is no way to avoid this as variables in Ruby are not containers: they point towards an object. However, it is your duty to make your code readable. If you see a constant that you cannot easily discern the value of, would you think that is readable?
We should talk about how Ruby loads constants and, more generally, files. Every Ruby application has its entry point. The interpreter will need the file to load and execute the commands of the application. The Ruby interpreter will increment over each statement inside your file and execute them following a specific set of rules, until it parses the entire file. What happens when the interpreter iterates to a constant? What other types of constants are there? Hint: CONSTANT_HERE is not the only constant you are defining.
The class keyword is processed first and the interpreter creates a constant 'TestingA' and stores in that constant a class object. The name of the class instance is "TestingA", a string, named after the constant. Yes, classes and modules are constants. Each class has a constant table, which is where "TestingA" will be stored. After this, the body of the class is interpreted and a new entry is created in the constant table for "CONSTANT_HERE" and the value is set and stored. This is happening before your definition of get_details has been defined: the interpreter wants to set the value, to store it, before "get_details" has been interpreted, which is why you are experiencing the error you are.
Now we know this, and wanting to provide an example of how constants are evaluated in code, you would need to mimic the below in order to have a method defined in a constant:
def get_details(file)
"stub_return"
end
class TestingA
CONSTANT_HERE = { details: get_details('example.json') }
end
In my opinion, the above is not good practise as it is an example mis-use of constant assignment. If you want to get details from a file, define a class/method and call the API/method instead. It's neater, assigns role of responsibility and is less ambiguous.

Why do I get "uninitialized constant" error when referencing a class?

Here is how my data structure looks like
Controller
API
V1
Controller1.rb
Controller2.rb
Serializers
Model1Serializer.rb
Model2Serializer.rb
I'm trying to access the serializers in my Controllers
Here is my Controller
class API::V1::Controller1 < ApplicationController
require_relative 'model1_serializer'
def doStuff
render json:MyData, each_serializer:Model1Serializer
end
end
Here is my serializer
class API::V1::Serializers::Model1Serializer < ActiveModel::Serializer
# Code here for serializing
end
I'm getting the following error. Why does it think Model1Serializer is under Controller 1?
uninitialized constant API::V1::Controller1::Model1Serializer
If In my Controller i change Model1Serializer to API::V1::Serializers::Model1Serializer then it works, except I don't want to be dependent on V1 in my namespace, that way If I decide to move the code to V2 I don't end up changing the code to point to V2. What's the best way to handle this?
"Why does it think Model1Serializer is under Controller 1:
Strictly speaking that is not actually the case. When looking up a constant, Ruby checks multiple namespaces (if they available) but the error message only mentions the innermost one.
Why do I get “uninitialized constant” error when referencing a class?
To answer this, it is important to understand how Ruby looks up constants. Assuming you have this namespace hierarchy:
module API
module V1
class Controller1
end
end
end
If you are accessing Model1Serializer from Controller1 Ruby checks the following nested namespaces:
API::VI::Controller1::Model1Serializer
API::VI::Model1Serializer
API::Model1Serializer
::Model1Serializer
But Model1Serializer is defined in API::VI::Serializers::Model1Serializer which is not included in this list. That is why Ruby can't find it.
To fix this, you should change the offending line to include the sub-module:
render json: MyData, each_serializer: Serializers::Model1Serializer
But most likely it still won't work because you are using "shortcut namespaces", i.e. API::V1 instead of module API; module V1; ...; end; end This prevents Ruby from searching parent namespaces because they are not added to the nested modules list.
In other words, only the following namespaces are checked by Ruby:
API::VI::Controller1::Serializers::Model1Serializer
::Serializers::Model1Serializer
You can access the module hierarchy by calling Module.nesting at the desired code location.
Disclaimer: There is a lot more to Ruby constant lookup than presented here.
Your controller is API::V1::Controller1, but your serializer is API::V1::Serializers::Model1Serializer.
In your doStuff method, you try to look up Model1Serializer -- but there's no way to see that class from this point in the namespace.
Try using API::V1::Serializers::Model1Serializer instead there.
Update: The OP edited his question:
If In my Controller i change Model1Serializer to API::V1::Serializers::Model1Serializer then it works, except I don't want to be dependent on the Vq in y namespace, that way If I decide to move the code to V2 I don't end up changing the code to point to V2. What's the best way to handle this?
A quick way is to define Current = V1 in your API module, then reference API::Current::... when referencing your models from the controllers.
A better way is to think carefully about why you need to simultaneously provide multiple APIs in the same application like this, and whether that's really the right way to version it. (That's probably outside the scope of a single SO answer, though, and will be too dependent on your specific application.)

See where a symbol is defined in irb

I work on a pretty large rails project at work. Sometimes I need to hunt down class / constant definitions. Is there some built-in method in Ruby to do this for me? Example:
irb> SOME_CONSTANT.__file__
=> /some/path/to/a/file
This isn't exactly what you're looking for, but methods do have a .source_location method on them. You can use this to find out where a class is actually implemented. (Since ruby lets you reopen classes, this could be in multiple places)
for example, given an instance of an object, i:
i.methods.map do |method_name|
method_obj = i.method(method_name)
file, line = method_obj.source_location
file #map down to the file name
end.uniq
will give you a list of all the files where i's methods are implemented.
This will work for classes that have at least 1 method implemented in ruby. It won't work for constants, though.
At the very beginning before any file is loaded, insert a line that defines the class/constant that you want to check as something other than a module. For example, suppose you have class or other kind of constant A within your code, and want to know where it is defined. Then, at the very beginning of the main file, write
A = nil
Then, when the program is run, whenever it first meets the definition of class/constant A, it will show something like
some_path_to_a_file:line_number in `some_method': A is not a class (TypeError)
or
some_path_to_a_file:line_number: warning: already initialized constant A
Then, some_path_to_a_file:line_number will be the location where A is defined.
If you're using Ruby 1.9.2, #YenTheFirst's answer is correct: call #source_location on a Method object.
If you're using Ruby 1.8.7, then #source_location doesn't exist (yet). You'll need something like this implementation of a method. (There's another one or two floating around, but I can't find the other one real quick).

How do I use parameters in a constructor in a rails model

I trying to learn tdd using RSpec. I took this example from a cheat sheet I found online and am a bit confused as to how I would implement it. To add MovieList.new is automatic but how would I go about adding a parameter when it is already handled with ActiveRecord. And then to add the 'forward' method as well.
describe "forward" do
it "should jump to a next movie" do
next_movie = MovieList.new(2).forward
next_movie.track_number.should == 2
end
end
If this is a test for a MovieList class, create a class called MovieList.
Then in your constructor for that class, make sure it takes in a parameter called track_number, in your test that's the 2.
Then create a method called forward to do whatever you need it to do?
Here's a good example of where I'm going with this:
http://rspec.info/
This may sound ambiguous, but so was the question.
EDIT:
This is a rough idea of how to create a new MovieList class and initialize it with a parameter called track_number.
def MovieList
attr_accessor :track_number
def initialize(track_number)
#track_number = track_number
end
# You can define all your class methods below, you
# can start with forward.
def forward
# do something...
end
end
movie = Movie.new(:track_number => 2)
movie.forward
I am not sure what forward does in your example because you seem to be initializing track_number to 2 then calling forward. I would have expected track_number to increment but your test is checking to see if it's 2 still.
Note, I don't believe you need to change your constructor to take the parameter as long as you pass it in as a hash (the single member hash is implied in my example)...can someone verify or refute this last assertion?

Resources