How to use Unix pager programs like `less` from Ruby? - ruby

Suppose I have a string called very_long_string whose content I want to send to the standard output. But since the string is very long, I want to use less to display the text on the terminal. When I use
`less #{very_long_string}`
I get File not found error message, and if I use:
`less <<< #{very_long_string}`
I get unexpected redirection error message.
So, how to use less from inside Ruby?

You could open a pipe and feed your string to less via its stdin.
IO.popen("less", "w") { |f| f.puts very_long_string }
(Assuming very_long_string is the variable holding your string.)
See: http://www.ruby-doc.org/core-1.8.7/IO.html#method-c-popen

A simple hack:
require 'tempfile'
f = Tempfile.new('less')
f.write(long_string)
system("less #{f.path}")

Although less can read text files its natural fit is to use it as the last command in a pipe. So a natural fit would be:
shell-command-1 | shell-command-2 | shell-command-3 | less
At your shell prompt:
echo tanto va la gatta al lardo che ci lascia lo zampino|less
..So you can try this in irb:
`echo tanto va la gatta al lardo che ci lascia lo zampino|less`
but I will prefer to use:
your_string = "tanto va la gatta al lardo che ci lascia lo zampino"
`echo "#{your_string}"|less`
If you have time read this SO question.
For a thorough demonstration of using system calls in ruby see this gist:
https://gist.github.com/4069

Related

Issue reading file containing Unicode chars using JavaProperties gem

I have mobile automation code in Ruby with locale property files and code is using JavaProperties::Properties.new(filename with path) which is returning hash and we are reading property value by providing property name.
Recently fr_CA.properties file was updated with unicode chars, something like "Solde du dernier relev\u00E9". After the update, I'm getting value "Solde du dernier relevé" instead of "Solde du dernier relevé".
I need some help how/where to provide UTF-8 conversion type.
Quick help highly appreciated.
#filePaths={
:pathTo_some_JavaProperties => #resourcesPath+"/service_"+locale+""+platform_fileName+".properties",
:pathTo_locale_other_JavaProperties => #resourcesPath+"/MoblClient_XmlService"+locale+".properties"
// more file paths
}
begin
#someHash = JavaProperties::Properties.new(#filePaths.fetch(:pathTo_some_JavaProperties))
rescue Errno::ENOENT
filesNotFound << #filePaths.fetch(:pathTo_some_JavaProperties)
end
// Reading value as #someHash['propName'] which is giving output as "Solde du dernier relevé"
Ok, here's what I am getting:
In test.properties:
item1 = Solde du dernier relev\u00E9
Then in Ruby,
> JavaProperties.load('test.properties')[:item1]
# => "item1 Solde du dernier relevé"
You should try getting your problematic code as stripped as possible, and then see if you keep getting the error.
BTW, I think you should use JavaProperties.load, not JavaProperties.new as in your sample.

Workaround for Scala Error: The command line is too long?

I'm trying to compile someone's mod of minecraft so that I can make some changes to it. It uses scala somewhere in the recompiling, and I get this error:
== ERRORS FOUND in SCALA CODE ==
The command line is too long.
================================
I tend to agree:
'"scalac.bat" -encoding UTF-8 -deprecation -target:jvm-1.6 -classpath "jars\vers
ions\1.8.1\1.8.1.jar;lib;lib\*;lib;lib\*;jars\bin\minecraft.jar;jars\bin\jinput.
jar;jars\bin\lwjgl.jar;jars\bin\lwjgl_util.jar;jars\libraries\net\java\jinput\ji
nput\2.0.5\jinput-2.0.5.jar;jars\libraries\org\lwjgl\lwjgl\lwjgl-platform\2.9.1\
lwjgl-platform-2.9.1-natives-windows.jar;jars\libraries\com\ibm\icu\icu4j-core-m
ojang\51.2\icu4j-core-mojang-51.2.jar;jars\libraries\tv\twitch\twitch-external-p
latform\4.5\twitch-external-platform-4.5-natives-windows-64.jar;jars\libraries\c
om\sixense\SixenseJavaLibrary\062612.0\SixenseJavaLibrary-062612.0-natives-windo
ws.jar;jars\libraries\org\apache\httpcomponents\httpcore\4.3.2\httpcore-4.3.2.ja
r;jars\libraries\org\apache\logging\log4j\log4j-api\2.0-beta9\log4j-api-2.0-beta
9.jar;jars\libraries\org\apache\commons\commons-lang3\3.3.2\commons-lang3-3.3.2.
jar;jars\libraries\net\java\jutils\jutils\1.0.0\jutils-1.0.0.jar;jars\libraries\
com\paulscode\libraryjavasound\20101123\libraryjavasound-20101123.jar;jars\libra
ries\net\sf\jopt-simple\jopt-simple\4.6\jopt-simple-4.6.jar;jars\libraries\com\g
oogle\guava\guava\17.0\guava-17.0.jar;jars\libraries\commons-logging\commons-log
ging\1.1.3\commons-logging-1.1.3.jar;jars\libraries\org\apache\commons\commons-c
ompress\1.8.1\commons-compress-1.8.1.jar;jars\libraries\com\sixense\SixenseJava\
062612.1\SixenseJava-062612.1.jar;jars\libraries\tv\twitch\twitch\6.5\twitch-6.5
.jar;jars\libraries\com\paulscode\codecjorbis\20101023\codecjorbis-20101023.jar;
jars\libraries\optifine\OptiFine\1.8.1_HD_U_B2\OptiFine-1.8.1_HD_U_B2.jar;jars\l
ibraries\com\paulscode\soundsystem\20120107\soundsystem-20120107.jar;jars\librar
ies\org\json\json\20140107\json-20140107.jar;jars\libraries\com\paulscode\librar
ylwjglopenal\20100824\librarylwjglopenal-20100824.jar;jars\libraries\org\lwjgl\l
wjgl\lwjgl_util\2.9.1\lwjgl_util-2.9.1.jar;jars\libraries\commons-codec\commons-
codec\1.9\commons-codec-1.9.jar;jars\libraries\org\apache\httpcomponents\httpcli
ent\4.3.3\httpclient-4.3.3.jar;jars\libraries\de\fruitfly\ovr\JRiftLibrary\0.4.4
.1\JRiftLibrary-0.4.4.1-natives-windows.jar;jars\libraries\org\lwjgl\lwjgl\lwjgl
\2.9.1\lwjgl-2.9.1.jar;jars\libraries\commons-io\commons-io\2.4\commons-io-2.4.j
ar;jars\libraries\com\mojang\realms\1.7.4\realms-1.7.4.jar;jars\libraries\net\ai
b42\mumblelink\JMumbleLibrary\1.1\JMumbleLibrary-1.1-natives-windows.jar;jars\li
braries\com\mojang\authlib\1.5.17\authlib-1.5.17.jar;jars\libraries\com\google\c
ode\gson\gson\2.2.4\gson-2.2.4.jar;jars\libraries\net\minecraft\launchwrapper\1.
7\launchwrapper-1.7.jar;jars\libraries\com\paulscode\codecwav\20101023\codecwav-
20101023.jar;jars\libraries\tv\twitch\twitch-platform\6.5\twitch-platform-6.5-na
tives-windows-64.jar;jars\libraries\net\java\jinput\jinput-platform\2.0.5\jinput
-platform-2.0.5-natives-windows.jar;jars\libraries\de\fruitfly\ovr\JRift\0.4.4.1
\JRift-0.4.4.1.jar;jars\libraries\org\apache\logging\log4j\log4j-core\2.0-beta9\
log4j-core-2.0-beta9.jar;jars\libraries\net\aib42\mumblelink\JMumble\1.0\JMumble
-1.0.jar;jars\libraries\io\netty\netty-all\4.0.23.Final\netty-all-4.0.23.Final.j
ar" -sourcepath src\minecraft -d bin\minecraft src\minecraft\*.java src\minecraf
t\net\minecraft\block\*.java src\minecraft\net\minecraft\block\material\*.java s
rc\minecraft\net\minecraft\block\properties\*.java src\minecraft\net\minecraft\b
lock\state\*.java src\minecraft\net\minecraft\block\state\pattern\*.java src\min
ecraft\net\minecraft\client\*.java src\minecraft\net\minecraft\client\audio\*.ja
va src\minecraft\net\minecraft\client\entity\*.java src\minecraft\net\minecraft\
client\gui\*.java src\minecraft\net\minecraft\client\gui\achievement\*.java src\
minecraft\net\minecraft\client\gui\inventory\*.java src\minecraft\net\minecraft\
client\gui\spectator\*.java src\minecraft\net\minecraft\client\gui\spectator\cat
egories\*.java src\minecraft\net\minecraft\client\gui\stream\*.java src\minecraf
t\net\minecraft\client\main\*.java src\minecraft\net\minecraft\client\model\*.ja
va src\minecraft\net\minecraft\client\multiplayer\*.java src\minecraft\net\minec
raft\client\network\*.java src\minecraft\net\minecraft\client\particle\*.java sr
c\minecraft\net\minecraft\client\player\inventory\*.java src\minecraft\net\minec
raft\client\renderer\*.java src\minecraft\net\minecraft\client\renderer\block\mo
del\*.java src\minecraft\net\minecraft\client\renderer\block\statemap\*.java src
\minecraft\net\minecraft\client\renderer\chunk\*.java src\minecraft\net\minecraf
t\client\renderer\culling\*.java src\minecraft\net\minecraft\client\renderer\ent
ity\*.java src\minecraft\net\minecraft\client\renderer\entity\layers\*.java src\
minecraft\net\minecraft\client\renderer\texture\*.java src\minecraft\net\minecra
ft\client\renderer\tileentity\*.java src\minecraft\net\minecraft\client\renderer
\vertex\*.java src\minecraft\net\minecraft\client\resources\*.java src\minecraft
\net\minecraft\client\resources\data\*.java src\minecraft\net\minecraft\client\r
esources\model\*.java src\minecraft\net\minecraft\client\settings\*.java src\min
ecraft\net\minecraft\client\shader\*.java src\minecraft\net\minecraft\client\str
eam\*.java src\minecraft\net\minecraft\client\util\*.java src\minecraft\net\mine
craft\command\*.java src\minecraft\net\minecraft\command\common\*.java src\minec
raft\net\minecraft\command\server\*.java src\minecraft\net\minecraft\crash\*.jav
a src\minecraft\net\minecraft\creativetab\*.java src\minecraft\net\minecraft\dis
penser\*.java src\minecraft\net\minecraft\enchantment\*.java src\minecraft\net\m
inecraft\entity\*.java src\minecraft\net\minecraft\entity\ai\*.java src\minecraf
t\net\minecraft\entity\ai\attributes\*.java src\minecraft\net\minecraft\entity\b
oss\*.java src\minecraft\net\minecraft\entity\effect\*.java src\minecraft\net\mi
necraft\entity\item\*.java src\minecraft\net\minecraft\entity\monster\*.java src
\minecraft\net\minecraft\entity\passive\*.java src\minecraft\net\minecraft\entit
y\player\*.java src\minecraft\net\minecraft\entity\projectile\*.java src\minecra
ft\net\minecraft\event\*.java src\minecraft\net\minecraft\init\*.java src\minecr
aft\net\minecraft\inventory\*.java src\minecraft\net\minecraft\item\*.java src\m
inecraft\net\minecraft\item\crafting\*.java src\minecraft\net\minecraft\nbt\*.ja
va src\minecraft\net\minecraft\network\*.java src\minecraft\net\minecraft\networ
k\handshake\*.java src\minecraft\net\minecraft\network\handshake\client\*.java s
rc\minecraft\net\minecraft\network\login\*.java src\minecraft\net\minecraft\netw
ork\login\client\*.java src\minecraft\net\minecraft\network\login\server\*.java
src\minecraft\net\minecraft\network\play\*.java src\minecraft\net\minecraft\netw
ork\play\client\*.java src\minecraft\net\minecraft\network\play\server\*.java sr
c\minecraft\net\minecraft\network\status\*.java src\minecraft\net\minecraft\netw
ork\status\client\*.java src\minecraft\net\minecraft\network\status\server\*.jav
a src\minecraft\net\minecraft\pathfinding\*.java src\minecraft\net\minecraft\pot
ion\*.java src\minecraft\net\minecraft\profiler\*.java src\minecraft\net\minecra
ft\realms\*.java src\minecraft\net\minecraft\scoreboard\*.java src\minecraft\net
\minecraft\server\*.java src\minecraft\net\minecraft\server\gui\*.java src\minec
raft\net\minecraft\server\integrated\*.java src\minecraft\net\minecraft\server\m
anagement\*.java src\minecraft\net\minecraft\server\network\*.java src\minecraft
\net\minecraft\src\*.java src\minecraft\net\minecraft\stats\*.java src\minecraft
\net\minecraft\tileentity\*.java src\minecraft\net\minecraft\util\*.java src\min
ecraft\net\minecraft\village\*.java src\minecraft\net\minecraft\world\*.java src
\minecraft\net\minecraft\world\biome\*.java src\minecraft\net\minecraft\world\bo
rder\*.java src\minecraft\net\minecraft\world\chunk\*.java src\minecraft\net\min
ecraft\world\chunk\storage\*.java src\minecraft\net\minecraft\world\demo\*.java
src\minecraft\net\minecraft\world\gen\*.java src\minecraft\net\minecraft\world\g
en\feature\*.java src\minecraft\net\minecraft\world\gen\layer\*.java src\minecra
ft\net\minecraft\world\gen\structure\*.java src\minecraft\net\minecraft\world\pa
thfinder\*.java src\minecraft\net\minecraft\world\storage\*.java src\minecraft\o
ptifine\*.java src\minecraft\optifine\json\*.java' failed : 1
Given that windows has a character limit on the command line.
I'm using windows 8.1 and scala 2.11.0.
I was wondering, is there a way to set this as an environment/path variable or something to avoid this?
What would I have to change? I'm very new to messing around with system stuff like this, but I think I know where scala is called in the scripts and this argument is given. If that helps.
scalac accepts an #file parameter containing a file with arguments in. So if you can modify whatever script is calling scalac.bat to instead put the arguments in a temporary file arguments.txt and then call scalac #arguments.txt that should work.

Convert markdown italics and boldface to latex

I want to be able to convert markdown italics and boldface to latex versions on the fly (i.e., give a text string(s) return a text string(s)). I thought easy. Wrong! (Which it still may be). See the sill buisness and error I tried at the bottom.
What I have (note the starting asterisk that's been escaped as in markdown):
x <- "\\*note: I *like* chocolate **milk** too ***much***!"
What I would like:
"*note: I \\emph{like} chocolate \\textbf{milk} too \\textbf{\\emph{much}}!"
I'm not attached to regex but would prefer a base solution (though not essential).
Silly business:
helper <- function(ins, outs, x) {
gsub(paste0(ins[1], ".+?", ins[2]), paste0(outs[1], ".+?", outs[2]), x)
}
helper(rep("***", 2), c("\\textbf{\\emph{", "}}"), x)
Error in gsub(paste0(ins[1], ".+?", ins[2]), paste0(outs[1], ".+?", outs[2]), :
invalid regular expression '***.+?***', reason 'Invalid use of repetition operators'
I have this toy that Ananda Mahto helped me make if it's helpful. You could access it from reports via wheresPandoc <- reports:::wheresPandoc
EDIT Per Ben's comments I tried:
action <- paste0(" echo ", x, " | ", wheresPandoc(), " -t latex ")
system(action)
*note: I *like* chocolate **milk** too ***much***! | C:\PROGRA~2\Pandoc\bin\pandoc.exe -t latex
EDIT2 Per Dason's comments I tried:
out <- paste("echo", shQuote(x), "|", wheresPandoc(), " -t latex"); system(out)
system(out, intern = T)
> system(out, intern = T)
\*note: I *like* chocolate **milk** too ***much***! | C:\PROGRA~2\Pandoc\bin\pandoc.exe -t latex
The lack of pipes on Windows made this tricky, but you can get around it using input to provide the stdin:
> x = system("pandoc -t latex", intern=TRUE, input="\\*note: I *like* chocolate **milk** too ***much***!")
> x
[1] "*note: I \\emph{like} chocolate \\textbf{milk} too \\textbf{\\emph{much}}!"
Noting I am working on windows and from ?system
This means that redirection, pipes, DOS internal commands, ... cannot be used
and the note from ?system2
Note
system2 is a more portable and flexible interface than system,
introduced in R 2.12.0. It allows redirection of output without
needing to invoke a shell on Windows, a portable way to set
environment variables for the execution of command, and finer control
over the redirection of stdout and stderr. Conversely, system (and
shell on Windows) allows the invocation of arbitrary command lines.
Using system2
system2('pandoc', '-t latex', input = '**em**', stdout = TRUE)

How to change the prompt

I am trying to configure the prompt characters in ripl, an alternative to interactive ruby (irb). In irb, it is done using IRB.conf[:DEFAULT], but it does not seem to work with ripl. I am also having difficulty finding an instruction for it. Please guide to a link for an explanation or give a brief explanation.
Configuring a dynamic prompt in ~/.riplrc:
# Shows current directory
Ripl.config[:prompt] = lambda { Dir.pwd + '> ' }
# Print current line number
Ripl.config[:prompt] = lambda { "ripl(#{Ripl.shell.line})> " }
# Simple string prommpt
Ripl.config[:prompt] = '>>> '
Changing the prompt in the shell:
>> Ripl.shell.prompt = lambda { Dir.pwd + '> ' }
ripl loads your ~/.irbrc file, which
typically contains some irb specific
options (e.g. IRB.conf[:PROMPT]). To
avoid errors, you can install
ripl-irb, which catches calls to the
IRB constant and prints messages to
convert irb configuration to ripl
equivalents.
http://rbjl.net/44-ripl-why-should-you-use-an-irb-alternative

command line arguments in bash to Rscript

I have a bash script that creates a csv file and an R file that creates graphs from that.
At the end of the bash script I call Rscript Graphs.R 10
The response I get is as follows:
Error in is.vector(X) : subscript out of bounds
Calls: print ... <Anonymous> -> lapply -> FUN -> lapply -> is.vector
Execution halted
The first few lines of my Graphs.R are:
#!/bin/Rscript
args <- commandArgs(TRUE)
CorrAns = args[1]
No idea what I am doing wrong? The advice on the net appears to me to say that this should work. Its very hard to make sense of commandArgs
With the following in args.R
print(commandArgs(TRUE)[1])
and the following in args.sh
Rscript args.R 10
I get the following output from bash args.sh
[1] "10"
and no error. If necessary, convert to a numberic type using as.numeric(commandArgs(TRUE)[1]).
Just a guess, perhaps you need to convert CorrAns from character to numeric, since Value section of ?CommandArgs says:
A character vector containing the name
of the executable and the
user-supplied command line arguments.
UPDATE: It could be as easy as:
#!/bin/Rscript
args <- commandArgs(TRUE)
(CorrAns = args[1])
(CorrAns = as.numeric(args[1]))
Reading the docs, it seems you might need to remove the TRUE from the call to commandArgs() as you don't call the script with --args. Either that, or you need to call Rscript Graphs.R --args 10.
Usage
commandArgs(trailingOnly = FALSE)
Arguments
trailingOnly logical. Should only
arguments after --args be returned?
Rscript args.R 10 where 10 is the numeric value we want to pass to the R script.
print(as.numeric(commandArgs(TRUE)[1]) prints out the value which can then be assigned to a variable.

Resources