Aliasing nil in ruby [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm not sure how I got on the concept but I've been thinking a lot about the properties of nil in ruby.
Let's say I want to write a gem that aliases nil to some other 'word.' Is just wrapping nil in a method in the global namespace the most effective way about this.
def bill
nil
end
or is there a more effective and cleaner way to go about doing this without polluting the global namespace?

Questions of bad practice aside, I don't see anything wrong with just putting a nil-returning method in a global namespace. As with many things in Ruby, this simple method gets the job done.
If you want to avoid "polluting the namespace," though, try putting your nil method and its ilk in their own module, and then using Ruby's include keyword to add them into the local namespace.
Hope that answers your question.

Related

Ruby objects without 'to_s' [closed]

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
Are there any objects in ruby that don't respond to to_s?
The question isn't meant to ask whether it is possible to create one, which I know can be done by undef_method. Feel free to explain details, including caveats of undefining.
The BasicObject class does not define a to_s method, so any instance of that class would not have a to_s method.
We should never specialize a subclass, since the subclass would not attend to the parent expectation anymore, have a look in the Liskov Substitution Principle.

Parse code file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
nd maybe there is some tool for this? I must extract entire blocks and subblocks like 'it'
I doubt any parser could help to load code blocks into variables. It would require eval, but even with eval it would be extremely hard to collect all the context etc.
It the target is rspec scenarios, I would go with monkeypatching rspec core, prepending your own detectors like:
def before(*args, &block)
MyCollector.collect_block(block)
super(*args, &block)
end
You can parse it with
https://github.com/seattlerb/ruby_parser
or
https://github.com/whitequark/parser
and will receive an AST (Abstrax Syntax Tree) which you then can process further. Depending on the amount of details you need from the source, you could also use some Regexps or write your own parser...
Perhaps you can tell us a little more about your project (input, output, reasons)

What module do the class `Hash`, `Array`, `File` belong to? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Simple question:
What module do the class Hash, Array, File belong to ?
Would you just say that they are part of the ruby core?
They are instances of the Class module (class).
Classes don't "belong to modules". They are just objects like any other object. If you are asking about the constants Hash, Array, File, etc., those belong to the Object class. All constants that are not explicitly defined in some specific module belong to Object.
The simple answer to the simple question is Yes they are part of Ruby Core
Yes. Hash, Array, File they all belongs to core.

Ruby detect if a column value has changed [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I have this line:
if self.company_changed?
And it works fine but this detects if the company has changed on the object. I need to know if the database value has changed and not if the value in memory has changed. So I tried this:
if :company_changed?
This seems to work in debug mode when I only execute the one line. If I let it run, it fails in testing on an infinite loop.
My question is what can be used in ruby to check to see if the column value has actually changed.
I'm pretty sure you're actually talking about ActiveRecord. In which case, you'd need to re-fetch the record to see if the value has changed in the database.
self.class.find(self.id).company != self.company
A general purpose method for this might be something like:
def attr_changed_in_db?(attr)
self.class.find(self.id).attributes[attr] != self.attributes[attr]
end
There is an excellent screencast on this by the great Ryan Bates.

How can I create a list of reserved words in a Ruby program? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I know that I can just browse the web for these words but is there a way to just let Ruby do it for me? You don't have to give me a whole program, just some hints to let me go about it. Thanks in advance.
Well, you can always take a look at the ruby's source code: keywords.
You can't get Ruby to do this for you.
However, here's the list from Dave Thomas's Programming Ruby 1.9, The Pragmatic Programmers' Guide (Also known as the Pickaxe):
__FILE__, __LINE__, BEGIN, END, alias, and, begin, break, case, class, def, defined?, do, else, elsif, end, ensure, false, for, if, in module, next, nil, not, or, redo, rescue, retry, return, self, super, then, true, undef, unless, until, when, while, yield

Resources