Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to design an edifact parser, I was planning on having one class to read the file, one class to map the data and then one other class to deal with data storage. The part where I am having a major problem is in how instances of those classes should communicate with each other. Any advice would be appreciated.
I don't see a need for the classes to communicate (pass messages) but would suggest some Strategy pattern be used.
You'll have a class to read the file and make sense of it's Syntax. For example, something which can handle whitespace and return formatted information like 'token', 'word' etc.
The class which reads and parses syntax is passed into the Semantic parser. The Semantic parser makes sense of the meaning. For example you might expect "Id, command, token, string" in that order. The Semantic parser might use a Command pattern.
The Semantic class outputs structured data, so is passed into your structure builder (builder pattern).
So your code might look like;
MyDataStructure = DataBuilder(SemanticParser(SyntaxParse(FileReader(filename))));
HTH
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I would like to generate booking reference numbers so they can easily be communicated by voice, written down, texted etc.
I currently use uuid, which works well, but results in poor UX, as strings like 510632cc-3aa4-41c2-8bd0-cbd2bd671ef4 are difficult to communicate.
Here is an Airbnb booking reference, which is nice and short and easy to communicate verbally:
Best practices
I gave it some thought and decided on using all capital letters except O (and no lower case letters), and the numbers 1-9.
This results in 25 + 9 = 34 possible characters. There could therefore be
34^6 = 1544804416 (1.5 billion) 6-character codes
Or if that isn't enough, 34^7 = 52523350144 (52 billion) 7-character codes
In my use case 6-characters is ample.
To summarise, the booking_reference should be a random string of letters A-Z except O, and numbers 1 -9 (not 0), no lower case letters. And the column must be unique - no duplicate strings.
Question
Have I encapsulated best practices in my ideas above, and, presuming I create a booking_reference column to store the string, how can I generate these in the bookings#create controller action?
Note: I'm not sure if it's relevant to the ruby tag; please feel free to remove that tag if it isn't relevant.
You should not do this in your controller.
This example below will generate a 8-character booking reference before creating your record, no zeros and Os. Note that it doesn't look for collisions, you should elaborate on that if you expect a lot of records, there are many different ways to do it including model validations and so on. You might also want to add an index on booking_reference to your table.
class YourModel < ApplicationRecord
# ...
before_create :generate_booking_reference
def generate_booking_reference
self.booking_reference = 8.times.map { [*'1'..'9', *'a'..'n', *'p'..'w'].sample }.join
# TODO check for collisions
end
# ...
end
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I have two orthogonal questions related to symbol tables:
Should I build the symbol table and perform type checking as I parse the code? Parsing first and then traversing the AST to build the symbol table looks cleaner to me. However, I like the idea of having an immutable AST (similar to Clang), and I can't have that in a two-step process (as I would need to insert extra type conversion nodes in the type checking phase).
Should the symbol table be responsible for doing type checking? I read multiple articles in which symbol tables are used for this purpose. Is that a recommended practice? It looks rather awkward to me.
Note: I am using a top-down recursive descent parser.
I believe this is what you should do:
First one: You should build your AST first, then, as you said traverse it to fill the symbol table up and do the type-checking. The immutable AST seems good, but it won't be as clean traversing the AST.
Second one: Yes, symbol tables should have a part in type-checking (not do type-checking). It will be needed to store the types of things like variables. There is nothing awkward about it :-)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
In Ruby there is a nifty method of creating an array of symbols.
Percent i method.
%i[a b c]
I am working in a team based environment, and although I can see this being useful with a large list of array, I have this is being implemented for everything
a = %i[] # Empty array
cancan abilities
can :manage, :all
cannot %i[create show update], AModel
cannot %i[create], BModel
cannot %i[show], CModel
Is it good to initialize all arrays using this methods or just
cannot [:create, :show, :update], AModel
I know there is no 'right' way but I would like the pros and cons to help me decide
In terms of the language itself, there are no important differences between the two, so it really comes down to a matter of preference. Personally, I think that the array of symbol literals ([:create, :show, :update]) looks more Ruby-like and that is what I'd use, but you should discuss it with your team. The most important part is being consistent, and you don't want to make the decision alone.
In the general case of when to use %i instead of writing out a literal array of symbols, I'd consider using the former if it was a list of symbols that had certain meaning, like a class constant or something, and there were more than just a couple. I've been using Ruby for a while now and until this question didn't really register the existence of %i, so it's relatively uncommonly used. It's maybe not in the average Rubyist's vocabulary, so that might be another reason to stay away from it.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Why are utility classes considered bad practice in iOS ? And categories used as a replacement instead of helper classes/utility classes. Is there any particular benefit that we get out of categories that we don't get from utility classes ?
Categories have a specific purpose. They extend functionality of a class in code that's external to the class for some reason (you don't have source for the original, you want different visibility for the category, ...).
When you say "helper" class, that sounds like delegates rather than categories...or just simple composition.
Actual utility classes -- ones that have no instances or state -- do exist where needed.
Utilities are not a bad practice in iOS. Sometimes, it makes sense to have them around if you need a central hub for useful functions with a specific common goal (i.e.: a MathUtils class for parsing doubles or ints in objective-c).
Having said that, by convention categories / extensions are considered nicer as they allow you to operate directly on the objects themselves without the need to allocate memory / instantiate other objects. For example, you can create a category on an NSNumber object to divide by a number easily, allowing you to have a language syntax that is easy to follow: i.e:
in swift:
number.divideBy(2)
or in objective-c
[number divideBy:2]
As opposed to:
let utility = UtilityClass()
utility.divideNumber(number, by:2)
Hopefully this helps convince you to start working with Categories, they are your best friends here!
One should not say that utility classes are worse at all. It depends on the task.
Maybe the reason for the statement is that developers coming from different programming languages don't know categories and simply use utility classes, even a category would do the job better. This esp. applies to utility classes, whose single purpose is to split an existing parent class into more lightweight parts. This is akin of bad, because it does not reflect the meaning of a class: If code is put into a class semantical correctly, you should not break the semantics for administrative reasons. It is a part of the class, let it be a part of the class.
There is a simple test for it: If you find yourself typing self.master (for master being the original class) very often (esp. this is the only usage of self at all) it is obvious that the utility class has no individual purpose and completely works on the original class.
But, of course, if you have a separate functionality, backed with a separate set of ivars, it might be correct to have extra classes for them. (Are they still utility classes? Maybe you should ask your Q more specific.)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am building a rails app that i need to update totals of managers each time someone is added. The method that updates the totals to me is looking really bad there is a lot of repetition going on that could be solved with a bit of metaprogramming. so i have a private method being called on before_save called update_totals
So we categorize them like this there is four races, african, coloured, indian and white there are three kinds of managements senior, middle and junior and then obviously two kinds of genders male and female. I need to be able to assign each possible variation of the three so i end up with something like this.
self.number_of_african_female_senior_managers = managers.native.african.female.senior.count
self.number_of_african_male_senior_managers = managers.native.african.male.senior.count
self.number_of_african_female_middle_managers = managers.native.african.female.middle.count
self.number_of_african_male_middle_managers = managers.native.african.male.middle.count
self.number_of_african_female_junior_managers = managers.native.african.female.junior.count
self.number_of_african_male_junior_managers = managers.native.african.male.junior.count
I would need to do this for each race. So i thought of building them dynamically and then having them assigned.
So something like this:
["african", "indian", "coloured", "white"].each do |race|
["senior","middle","junior"].each do |management_type|
["male","Female"].each do |gender|
"number_of_#{race}_#{gender}_#{management_type}_managers" = managers.native.race.gender.management_type.count
end
end
end
But this will return strings and not variables that be assigned. I saw there is a define_method method that can dynamically build methods but that looks like you have to call it outside of the constructor in this example, and i saw that you can use Object#send like this example, but i cant see that working in this situation either.
Is this a good ruby practice? Its adding a bit of complexity but removing a lot of DRY code.
Is this a good ruby practice?
DRY is a good ruby practice, so the answer to your question is: YES
Regarding your code you will probably use send in such a situation.
something like self.send("number_of_#{race}_#{gender}_#{management_type}_managers=", managers.send(native).send(race).send(gender).send(management_type).count)
This still looks pretty ugly to me. I am kind of asking myself why that datastructure is so insane. Why are you not using a simple hash structure here?