How to pass an argument when calling a view file?
You can pass a hash of parameters to the Haml method using the :locals
key:
get '/' do
haml :index, :locals => {:some_object => some_object}
end
This way the Ruby code in your Haml file can access some_object
and render whatever content is in there, call methods etc.
How to access content of a file pass as an argument?
<
is a redirection operator provided by your shell.
This operator opens the file on its right-hand side for reading, and, by default, remaps the standard input (stdin
) of your program to be this file.
It is also possible to specify which file descriptor to remap with the form:
program [n]< file
wherein [n] is a number. The typical mapping between UNIX file descriptors and C Standard Library streams is
0
-stdin
1
-stdout
2
-stderr
but it is possible to open other file descriptors, and have your program read from them with man 2 read
.
Here's an example program. Note the differences between these two different sets of commands:
This
./a.out a b < file.txt
will print the strings a
and b
under the section listing the program arguments, and then it will print the contents of file.txt
.
Whereas this
./a.out a b file.txt
will print the strings a
, b
, and file.txt
under the section listing the program arguments, and then wait for you to type something in the terminal.
#include <stdio.h>
int main(int argc, char **argv) {
size_t line = 0;
char buffer[256];
puts("--- argv ---");
for (int i = 0; i < argc; i++)
printf("arg #%d : %s\n", i, argv[i]);
puts("--- stdin ---");
while (fgets(buffer, sizeof buffer, stdin))
printf("%4zu|%s", ++line, buffer);
}
How can I pass an argument while writing a file using python
You can get arguments by calling sys.argv[]
array.
sys.argv[0]
means script name itself.
You can then write it back to your file, open the file such as here.
Pass arguments when using the File protocol
I am assuming you are using Windows? If so, there is no way to pass a parameter using the "file://" syntax, as it is an Asynchronous Pluggable Protocol that does not accept parameters.
However, if you really need it, you can craft your own pluggable protocol that accepts parameters.
Here's an example:
An Asynchronous Pluggable Protocol Handler for data: URLs
Passing a file as a command line argument and reading its lines
...
// command line parameter
if(argv.length != 1) {
System.err.println("Invalid command line, exactly one argument required");
System.exit(1);
}
try {
FileInputStream fstream = new FileInputStream(argv[0]);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get the object of DataInputStream
...
> java -cp ... Zip \path\to\test.file
How to pass text file as an argument in Ruby
On your shell, invoke the ruby
script followed by the name of the .txt
file, like this:
ruby foo.rb test_list.txt
The variable ARGV
will contain references to all the arguments you've passed when invoking the ruby
interpreter. In particular, ARGV[0] = "test_list.txt"
, so you can use this instead of hardcoding the name of the file:
File.open(ARGV[0]).each do |line|
puts line
end
On the other hand, if you want to pass the content of the file to your program, you can go with:
cat test_list.txt | ruby foo.rb
and in the program:
STDIN.each_line do |line|
puts line
end
Creating a function that takes in a text file as an argument
Can I recommend you try using Linq? e.g.:
Boolean found = File
.ReadLines(@"C:\MyFile.txt") // <- Your file name
.Any(line => line.Contains(word_to_find)); // <- Word to find
With Linq you don't need any methods to implement and have one line solution.
EDIT: in case that you have to find out words, e.g. "the"
, but not "then"
you can modify the solution by using regular expressions instead of simple Contains()
:
Boolean found = File
.ReadLines(@"C:\MyFile.txt")
.Any(line => Regex.IsMatch(line, @"(^|\W)" + word_to_find + @"($|\W)"));
passing files and values as parameter to a function in python
I would create a little class (give it a useful name) to encapsulate your data.
If your files grow you only have to change your create_lats
min_length = 1
max_length = 30
# delays
delay = 100
# Speed of light
c_vaccum = 3e8
#Little class to keep our data in one place
class Lat:
def __init__(self, filename, factor):
self.filename = filename
self.factor = factor
self.file = open(filename, "w") #let the class open the file
#now our function needs only one parameter, neat!
def latcalc(lat):
target_name = 0
for length in range(min_length, max_length):
if length < 2:
target_name += (length / (lat.factor * c_vaccum)) #acces the class variable
elif length == 2:
target_name += delay
else:
target_name = target_name
myline = "%s\t%s\n" % (length, target_name)
lat.file.write(myline)
def create_lats():
lats = []
lats.append(Lat("file1.txt", 0.4))
lats.append(Lat("file2.txt", 0.8))
lats.append(Lat("file3.txt", 1))
return lats
#loop over your lats created in create_lats
for lat in create_lats():
latcalc(lat)
lat.file.close() #close the file
How do I use a FILE as a parameter for a function in C?
You are lacking a function prototype for your function. Also, write
is declared in unistd.h
so that is why you get the first error. Try renaming that to my_write
or something. You really only need the stdio.h
library too as a side note, unless you plan on using other functions later. I added error checking for fopen
as well as return 0;
which should conclude every main function in C.
Here is what I would do:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
void my_write(FILE *fp, char *str)
{
fprintf(fp, "%s", str);
}
int main(void)
{
char *str = "test text\n";
FILE *fp;
fp = fopen("test.txt", "a");
if (fp == NULL)
{
printf("Couldn't open file\n");
return 1;
}
my_write(fp, str);
fclose(fp);
return 0;
}
Related Topics
Ruby - Array.Join Versus String Concatenation (Efficiency)
Running Phantomjs from a Ruby on Rails Application
When and Where Do I Require Files in a Rails Application
What Is the Point of Object#Presence in Rails
Ruby: How to Calculate a Path Relative to Another One
How to Run a Simple Ruby Script in Any Web Server (Apache or Mongrel or Any Thing Else)
Rails: Difference Between Env.Fetch() and Env[]
Decoding Facebook's Signed Request in Ruby/Sinatra
Rspec View Undefined Method Stub_Model
How to Get Rspec to Run All Tests Nested Under a Folder
"Certificate Verify Failed" Error When Installing Ruby Gems on Windows
(Ruby) How to Check Whether a Range Contains a Subset of Another Range
Ruby, Using Regex to Find Something in Between Two Strings