Parse code file [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 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)

Related

How to convert [u8] to [u32]? [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 last year.
Improve this question
I wanted to embed an image to my binary and used the "include_bytes" macro. The GUI library I wanted to use only accepts [u32] for input and the said macro produces only [u8].
How do I convert a [u8] to a [u32]? I've seen some in the internet but the explanations are a bit too technical for me (I'm only self-taught). There were several options that I saw like bitwise and a method in "u32" from the standard library. Anyone can give an actual code on how to do it? Like study it from there in case I will need it for other files in the future. Thank you. I almost always just understand things via code coz I'm not aware of many technical terms, algos, etc.
using .map(Into::<u32>::into)
fn main() {
assert_eq!([0_u8, 1_u8].map(Into::<u32>::into), [0_u32, 1_u32]);
}

Replacing memory references/addresses from a string containing <ClassName:MEM_REF> type parts [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 4 years ago.
Improve this question
I have strings like:
NoMethodError: undefined method 'sort_by!' for #<Hash:0x00007f98f03c84e0>
These strings can contain n number of such parts: <Hash:0x00007f98f03c84e0>.
Here, 0x00007f98f03c84e0 is just a placeholder of memory reference. And also Hash is type of object of which this memory reference is. There is no need to discuss how these strings got formed but in the end i have strings which can have anything like <ClassName:MEM_REF> and i have to replace MEM_REF part.
Going back to my original example, I want to remove this memory ref part 0x00007f98f03c84e0 with any string of my liking. Again, 0x00007f98f03c84e0 is an example, it will be any arbitrary memory address.
Looking for an elegant way of doing this in ruby.
Try following regex in ruby console, should work: /:[0-9]x[0-9A-Za-z]*(?=>)/.
And to mask these refs with anything else, try input_string.gsub!(/:[0-9]x[0-9A-Za-z]*(?=>)/, "REPLACE_TEXT")

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.

Should I use File.write? [closed]

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 5 years ago.
Improve this question
I recently run into problems converting a ruby script to .EXE because I had a File.write statement in it. The documentation doesn't mention the write method but when I do a
pp File.methods
it is there. So should I use File.write? In a normal Ruby script the following works
File.write("test.txt", "test")
But is it good practice and why doesn't the documentation mention it?
File.write is in fact IO.write (File is a child of IO) which can be verified by monkey-patching:
class IO
def IO.write
puts "IO's class method write was called."
end
end
File.write # outputs "IO's class method write was called."
It is very well in the documentation.
Thus, I see no reason not to use it.

Aliasing nil in ruby [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'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.

Resources