RUBY : Unexpected end of file and command not found? - ruby

I start to learn Ruby and i got some problem when i ran the code on the
Terminal. it says :
desktop/RUBY/boucles.rb: line 1: voyages: command not found
desktop/RUBY/boucles.rb: line 14: syntax error: unexpected end of file.
My IDE is sublim text.
How i can solve that ?
voyages = [
{ ville: "Paris", duree: 10 },
{ ville: "New York", duree: 5 },
{ ville: "Berlin", duree: 2 },
{ ville: "Montreal", duree: 15 }
]
voyages.each do |voyage|
if voyage[:duree] <= 5
puts "Voyage à #{voyage[:ville]} de #{voyage[:duree]} jours"
end
end

Your code is correct and works without any syntax errors. If you were editing in Sublime Text, you might want to make sure that you saved the file first.

Those errors look like they might be coming from your shell, not from Ruby!
How are you running this script? You should be running ruby boucles.rb in your terminal.

Related

VSCode keybinding that inputs large amount of code (Python: Visual Studio Code)

I'm currently trying to add a keybinding in Vscode that inputs multiple lines of code. Below is my current attempt at doing that – but it's not working due to some syntaxical errors. Would anyone know how I'm supposed to format this?
{
"key": "ctrl+y",
"command" : "type",
"args": {
"text": "
leave1 = False
while(leave1 == False):
try:
driver.find_element(By.XPATH, '')
except Exception:
time.sleep(0.5)
else:
leave1 = True
"
}
}

Ruby key / value for each loop debug

For various reasons I have a chef recipe that I want to use add multiple groups to a system with. I've defined a data bag with a key / value structure that provides the group name as the key and the guid as the value eg:
{
"id": "default_groups",
"group": {
"sales": "200",
"marketing": "800",
"csr": "1000",
"devel": "9000",
"scientists": "500"
}
}
I was wanting to use the group resource in a for each loop but I don't seem to be referencing the data in the data bag correctly:
user_groups = data_bag_item('groups', 'default_groups')
%w{"#{user_groups['group']}"}.each do |usr|
group "#{usr}.key" do
action :create
gid "#{usr}.value"
end
end
Can anyone explain what I'm doing wrong with my syntax?
The error output from the chef run is as follows:
ERROR: group["#{user_groups['group']}".key] (cookbook_groups::recipe_groups line 10) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '3'
---- Begin output of ["groupadd", "-g", "\"\#{user_groups['group']}\".value", "\"\#{user_groups['group']}\".key"] ----
STDOUT:
STDERR: groupadd: invalid group ID '"#{user_groups['group']}".value'
---- End output of ["groupadd", "-g", "\"\#{user_groups['group']}\".value", "\"\#{user_groups['group']}\".key"] ----
Ran ["groupadd", "-g", "\"\#{user_groups['group']}\".value", "\"\#{user_groups['group']}\".key"] returned 3
You cannot combine string interpolation, word arrays and array/hash access like that.
This might work:
user_groups = data_bag_item('groups', 'default_groups')
user_groups['group'].each do |key, value|
group key do
action :create
gid value
end
end
Apparently, %w does not do what you think:
▶ %w{"#{user_groups['group']}"}
#⇒ ["\"\#{user_groups['group']}\""]
You probably need just:
user_groups['group'].each
or like.

Error reading project.json, Unterminated string error when performing dotnet restore

Using Visual Studio for Mac to perform a "dotnet restore" but the terminal shows this message:
error : Error reading
'/Users/abc/corefx/src/Microsoft.TargetingPack.Private.CoreCLR/ref/project.json' at line 8 column 1 : Unterminated string. Expected delimiter: ". Path 'frameworks', line 8, position 1.
error:Unterminated string. Expected delimiter: ". Path 'frameworks', line 8, position 1.
project.json:
{
"dependencies": {
"Microsoft.TargetingPack.Private.CoreCLR": "1.2.0-beta-24904-03"
},
"frameworks": {
"netcoreapp1.1.0”: {}
}
}
I have found the file but I cannot identify the cause of the problem. What could cause this error message?
It looks to me like the netcoreapp1.1.0 property name is terminated with a typographic right quote mark ” which is not valid JSON. It should be a standard straight quote " instead.
{
"dependencies": {
"Microsoft.TargetingPack.Private.CoreCLR": "1.2.0-beta-24904-03"
},
"frameworks": {
"netcoreapp1.1.0": {}
}
}

Syntax Error When Writing a Multi-Dimensional Array to Excel File

I am attempting to write a multi-dimensional array to an Excel file (extension .xls) using the WriteExcel Ruby gem on El Capitan.
My attempt:
# -*- coding:utf-8 -*-
require 'writeexcel'
# Create a new Excel workbook
workbook = WriteExcel.new('ruby.xls')
# Add a worksheet
worksheet = workbook.add_worksheet
eec = [
['maggie', 'milly', 'molly', 'may' ],
[13, 14, 15, 16 ],
['shell', 'star', 'crab', 'stone']
]
worksheet.write_col('A1', \#eec)
workbook.close
The error I get:
iMac:scrapScripts guy$ ruby script.rb
script.rb:16: syntax error, unexpected $undefined, expecting ')'
worksheet.write_col('A1', \#eec)
^
Using the gem documentation found here.
Can anyone explain why this syntax error is occurring, and how I can fix it?
Remove garbage symbols from the call to write_col:
- worksheet.write_col('A1', \#eec)
+ worksheet.write_col('A1', eec)

Cannot parse multiple files with logstash input "file"

I am trying to parse a folder with logstash on windows but I have some strange results.
I have a file samplea.log with the following content :
1
2
3
4
5
I have a file sampleb.log with the following content:
6
7
8
9
10
This is my config file:
input {
file {
path=> "C:/monitoring/samples/*.log"
}
}
output {
stdout { debug => "true" }
elasticsearch {
protocol => "transport"
host => "127.0.0.1"
}
}
For unknown reasons, the events displayed on my console are 6,7,8,9 and the same events are stored in elasticsearch. The last line of my file sampleb is ignored and the whole file samplea is ignored.
Thanks in advance for your help.
To solve this problem, I've patched original logstash (1.4.2) distribution with the filewatch-0.6.1 gem (original is filewatch-0.5.1)
Logstash is delimiting lines with return character. Make sure you hit enter on the last line.
Known bug, the inner library tracks the file with inode. On Windows the library use a function that always return 0. In your case, I think logstash reads the simpleb.log, then reads simplea.log from index 6, which is end-of-file.
Bug tracked here : https://github.com/logstash-plugins/logstash-input-file/issues/2

Resources