Ruby snippet for reading lines block [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 3 years ago.
Improve this question
I am new to ruby and seeking help.
We got the csv files having the column with data shown below. Using the csv parser and fetch the column data for each in a variable.
And we want to change column data from csv file to line shown below and write to a file:
FROM
---
- - status
- New
- Delivered
TO
status from New to Delivered
Thanks #igian for help
---
- - status
- New
- Delivered
- - Milestone
- Sprint1
- Sprint
I was struggling for this. I tried to use y.second but it fails, please correct what I am doing wrong.

Looks like a YAML file, see https://ruby-doc.org/stdlib-2.6.2/libdoc/yaml/rdoc/YAML.html
Load the file then handle the array:
require 'yaml'
y = YAML.load_file( 'the_file.yaml' )
y #=> [["status", "New", "Delivered"]]
words = y.first
p "#{words[0]} from #{words[1]} to #{words[2]}"
#=> "status from New to Delivered"

Related

Remove string from each item on array using VB [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.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Basically I've an array that looks like this
array = ("Hello_123","Hello_234","Hello_345")
I would like to remove the word "Hello_" from the array so that the result would become
array = ("123","234","345")
Any idea how can I do that please?
As stated by #user692942, loop through the array and update the values using Replace() like this:
MyArray = Array("Hello_123","Hello_234","Hello_345")
For i = 0 to UBound(MyArray)
MyArray(i) = Replace(MyArray(i),"Hello_","")
Next

In FreeMarker adding one hour to current timestamp [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
In FreeMarker, I am trying to add one hour to a timestamp which is in the format "2016-08-11T20:06+06:00".
I have tried and searched on Google, but didn't find way to add hours to the current timestamp. The new timestamp should be current system timestamp plus one hour.
I am getting the following error.
For "." left-hand operand: Expected a hash, but this has evaluated to a date_or_time_or_datetime (wrapper: f.t.SimpleDate):
==> current [in nameless template at line 2, column 19]
FTL stack trace ("~" means nesting-related):
- Failed at: #assign newDate = current.add(10, 1) [in nameless template at line 2, column 1]
<#assign current= .now> <#assign newDate= current.add(10,1) > ${newDate}
I have tried this but getting above error. Please help me out.

Split json file into multiple flowfile [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 a json file such as below, i need to split this flowfile into numbers of flowfile as per each line
Input flowfile:
{a:122, b: 12, c: dev}
{b: 19, c: dev}
{a:111, b: 12, c: roman,d: 2.3}
Output Flowfile will have 3 files with each row.
Splitjson is just just spliting the first line, please suggest
Do you have downstream processors that expect one JSON per flow file? Otherwise you may be able to skip the Split entirely and just use the Record processors (ConvertRecord, PutDatabaseRecord, e.g.). The JsonTreeReader (in later versions of NiFi) accept the one-JSON-per-line format (even though that's not valid JSON per se). If you do need one JSON object per flowfile, Bryan's suggestion of SplitText with a Line Count of 1 is spot-on.
SplitText with line count of 1

how to add list of strings to a text file in ruby [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have list of strings. I am trying to append those string values to a text file.
Here is my code:
java_location = "#{second}#{first}"
The output of java_location is:
1.6.0_43/opt/oracle/agent12c/core/12.1.0.4.0/jdk/bin/java
1.6.0_43/opt/oracle/agent12c/core/12.1.0.4.0/jdk/jre/bin/java
1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java
1.6.0_35/u01/app/oracle/product/Middleware/Oracle_BI1/jdk/jre/bin/java
I want this output writing into a text file.
How can i do that?
File.write('file.txt', java_location)
You want to open the file in append mode ('a') rather than readwrite ('w+') which truncates the existing file to zero length before writing
http://alvinalexander.com/blog/post/ruby/example-how-append-text-to-file-ruby
if first && second
java_location = "#{second}#{first}"
a << java_location
File.open("/home/weblogic/javafoundmodified.txt", 'a') do |file|
a.each {
|item|
file.puts item
}
end
end

Ruby/Watir : How do I reference multiple .yml files from the same Ruby 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 9 years ago.
Improve this question
I am trying to retrieve key value pairs defined on two different .yml files. Is it possible to do so in a single Ruby file ?
Sure. Try this:
require 'yaml'
file1 = YAML.load_file("/home/abdo/settings.yml")
file2 = YAML.load_file("/home/abdo/database.yml")
This is an example I'm using in Rails to load a settings file:
SETTINGS = YAML.load_file("#{Dir.pwd}/config/settings.yml")[Rails.env]
If you want to load multiple files in 1 hash, you can do the following:
files = %w(database.yml settings.yml)
yamls = files.map { |f| YAML.load_file("#{Dir.pwd}/config/#{f}") }
H = files.each_with_object({}).with_index { |(e, hash), i| hash[e] = yamls[i] }
You can access H["database.yml"] to get the Hash representing the file with name database.yml
If you want to load a list of files following a certain pattern in a directory, you can use Dir.glob as mentioned in Iterate through every file in one directory
EDIT If your YAML files have non-conflicting data (data that does not get overridden when merged) and you'd like to merge all of them in a single Hash, you can do:
yamls.inject({}) { |hash, yaml| hash.merge(yaml) }

Resources