ruby way - relative path [duplicate] - ruby

This question already has answers here:
How to do a safe join pathname in ruby?
(2 answers)
Closed 5 years ago.
I am using File.read('file.txt') in a ruby script. But I don't know what is the best way to create a correct relative path, I can do
File.read(File.dirname(__FILE__) + '/file.txt)
but I wonder if there a more beautiful way in ruby?

There's a number of ways, but this one is the most flexible:
File.read(File.expand_path('file.txt', File.dirname(__FILE__))
You may also want to use Dir.pwd to be relative to the current working directory in your shell.
Note that using string concatenation for paths may work, but using expand_path or join is more reliable and properly handles platform differences, like on Windows where parts are natively joined with backslash instead.

Related

How can I access an xpath with a colon in the name? [duplicate]

This question already has answers here:
How does XPath deal with XML namespaces?
(2 answers)
Closed 2 months ago.
I am trying to access the contents of the path below:
/newsMessage/itemSet/newsItem[2]/contentSet/remoteContent[3]/rtr:altId
However, the path is not being accepted due to the colon, is there any way to escape this character or make the contents of this path accessible.
I have tried using something similar to this:
/*[name()='rtr:altId']
however I'm unsure where I would put this in the CSS and if I have formatted correctly.
Try this:
/newsMessage/itemSet/newsItem[2]/contentSet/remoteContent[3]/*[name()="altId"]
or
/newsMessage/itemSet/newsItem[2]/contentSet/remoteContent[3]/*[local-name()="altId"]

How to use escape sequences [duplicate]

This question already has answers here:
Ansi colours on Windows 10, sort of not working
(2 answers)
Closed 3 years ago.
I am trying to get escape sequences to work in a Go application. Specifically the code to move the cursor. fmt.Printf("\033[3;5H")
What should I be printing / writing to in order to make this work? Or am I approaching this the wrong way entirely? Every time I run the code I just get some funky looking characters in the terminal or nothing happens. I am running windows 10.
I have tried using multiple different fmt.Print functions but I get the same results.
// Move the cursor
fmt.Printf("\033[3;5H")
// Print at new position
fmt.Printf("Print this text at the new cursor position")
Turns out you need to enable Virtual Terminal Processing in Windows 10 to use ANSII escape codes
https://github.com/konsorten/go-windows-terminal-sequences

What does wc do? And how do you use it to count words in a file? [duplicate]

This question already has answers here:
Whats is the behaviour of the "wc" command?
(2 answers)
Closed 6 years ago.
I'm new to the Linux/Unix world but found this decent online resource https://linuxjourney.com that's sort of explains some general command line code. I'm running an Oracle VM with Ubuntu based variant. I'm away from my computer now so I don't know which version but I'm fairly certain it's bash4
WC (Word Count) is a simple command line utility that displays the number of words in a file. It is especially useful for text files. Word documents, Libre office documents etc are a different matter.
Simply type 'wc ' and it will output the number of words in the file. If you need more information, type 'man wc' in a terminal and it will show you the full list of options.

Check if file matching regex, glob, or wildcard pattern exists? [duplicate]

This question already has an answer here:
Check if a file exists using a wildcard
(1 answer)
Closed 7 years ago.
I'm aware of File.exists?, but that appears to operate with strings or IO objects. What I need is the ability to use a regex, glob, or wildcard.
For example, I want to say File.exists?("/pictures/tank_man.*"), to see if an image named tank_man exists, whether it's a jpg, tif, png, whatever.
You can use Dir["/pictures/tank_man.*"]
It will return existing paths

Change text colour in WINDOWS command line [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to have multiple colors in a batch file?
Colorizing Windows command line output from PHP
Let me just make this very clear: I am on WINDOWS, so \033[0;35mText WILL NOT WORK.
Sorry to have to emphasize like that, but every possible duplicate I have seen has people saying to use the above code regardless of what OS the question is about.
Now, I know it's possible without too much trouble - ffmpeg does exactly the kind of thing I want to do:
(source: adamhaskell.net)
So how hard can it be?
Use SetConsoleTextAttribute to set the text colour.

Resources