Ruby - undefined local variable or method `_1' - ruby

I'm getting the error undefined local variable or method '_1' for main:Object when I run the following code. Any idea why I'm getting this error? and how can I store the result in a variable.
result="-e hostname=webserver001 -e username=root -e password=testing123"
p result.scan(/\w+=\w+/)
.map { _1.split("=") }
.to_h
Error:
Traceback (most recent call last):
2: from main.rb:4:in `<main>'
1: from main.rb:4:in `map'
main.rb:4:in `block in <main>': undefined local variable or method `_1' for main:Object (NameError)
exit status 1

You're probably running an older version of ruby - the numbered parameters for blocks have been available in ruby since 2.7 - see https://www.bigbinary.com/blog/ruby-2-7-introduces-numbered-parameters-as-default-block-parameters
You can make it work on older version by using
p result.scan(/\w+=\w+/)
.map { |s| s.split("=") }
.to_h

Related

Ruby exception occurred: undefined method `split' for nil:NilClass

I have a trouble in logstash.
I wrote following code:
ruby {
code => 'separated = event.get("[request_url]").split("/")
separated.each_index {|x|
temp = "P" + x.to_s
event.set(temp, separated[x])}'
}
and I got following error:
[2021-07-26T16:27:37,135][ERROR][logstash.filters.ruby ][main][23b8bf19dac851a85d8c4d86162422f06362d00bf13b156291eea5b597c7f7ec] Ruby exception occurred: undefined method `split' for nil:NilClass {:class=>"NoMethodError", :backtrace=>["(ruby filter code):2:in `block in filter_method'", "/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-filter-ruby-3.1.7/lib/logstash/filters/ruby.rb:93:in `inline_script'", "/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-filter-ruby-3.1.7/lib/logstash/filters/ruby.rb:86:in `filter'", "/usr/share/logstash/logstash-core/lib/logstash/filters/base.rb:159:in `do_filter'", "/usr/share/logstash/logstash-core/lib/logstash/filters/base.rb:178:in `block in multi_filter'", "org/jruby/RubyArray.java:1809:in `each'", "/usr/share/logstash/logstash-core/lib/logstash/filters/base.rb:175:in `multi_filter'", "org/logstash/config/ir/compiler/AbstractFilterDelegatorExt.java:134:in `multi_filter'", "/usr/share/logstash/logstash-core/lib/logstash/java_pipeline.rb:295:in `block in start_workers'"]}
and I don't know what the problem is!
Can anyone help me?
I should test the value before using it.
So that I changed the code to this:
ruby {
code => 'urls = event.get("[request_url]")
if urls
separated = urls.split("/")
if separated
separated.each_index {|x|
temp = "P" + x.to_s
event.set(temp, separated[x])
}
end
end'
}
and the error fixed.

ruby error of "block in initialize: uninitialized constant" but running well with irb

I have a ruby script as following:
class HashSet < Hash
def initialize
super { |hash, key| hash[key] = Set.new }
end
end
data = {}
data["hash"] ||= HashSet.new
data["hash"]["colors"].add "blue"
puts data
An error is raised when run this script:
$ ruby demo.rb
Traceback (most recent call last):
2: from demo.rb:9:in `<main>'
1: from demo.rb:9:in `[]'
demo.rb:3:in `block in initialize': uninitialized constant HashSet::Set (NameError)
But when I run it with irb, it runs well:
$ irb -r ./demo.rb
{"hash"=>{"colors"=>#<Set: {"blue"}>}}
What makes the difference and how can I fix the script?
Add this to the top of your script:
require "set"
Explanation:
Set is not part of the ruby core library. Rather, it is part of the ruby standard library.
In order to use Set, you must - somewhere - require the library explicitly.
As it happens, irb actually already requires set as part of its initialisation process:
$ irb
irb(main):001:0> Set
=> Set
$ ruby -e 'Set'
Traceback (most recent call last):
-e:1:in `<main>': uninitialized constant Set (NameError)

I am getting an error on line 13. It says that the `<<` method is undefined. how do I fix this? I am using Ruby

This is the code:
def self.scrape_shoe
#doc.css("div.product-card__body").each do |nike|
name = nike.css("div.product-card__title").text.strip
price = nike.css("div.product-card__price").text.strip
shoes = self.new
#all << shoes #having error here
end
end
This is the error:
Traceback (most recent call last):
7: from bin/new_nikes:7:in `<main>'
6: from /home/merkonical/new_nikes/lib/new_nikes/cli.rb:17:in `call'
5: from /home/merkonical/new_nikes/lib/new_nikes/cli.rb:11:in `list_price'
4: from /home/merkonical/new_nikes/lib/new_nikes/scraper.rb:9:in `scrape_shoe'
3: from /home/merkonical/.rvm/gems/ruby-2.6.1/gems/nokogiri-1.10.4/lib/nokogiri/xml/node_set.rb:237:in `each'
2: from /home/merkonical/.rvm/gems/ruby-2.6.1/gems/nokogiri-1.10.4/lib/nokogiri/xml/node_set.rb:237:in `upto'
1: from /home/merkonical/.rvm/gems/ruby-2.6.1/gems/nokogiri-1.10.4/lib/nokogiri/xml/node_set.rb:238:in `block in each'
/home/merkonical/new_nikes/lib/new_nikes/scraper.rb:13:in `block in scrape_shoe': undefined method `<<' for nil:NilClass (NoMethodError)
What are possible fixes I can do for this?
I am working on Ruby
What are possible fixes I can do for this?
#all is nil. nil doesn't have a method named <<. Make sure #all is not nil.
Getting error because you are pushing shoes object into undefined instance variable.
Define #all in action.
def self.scrape_shoe
#doc.css("div.product-card__body").each do |nike|
#all = []
name = nike.css("div.product-card__title").text.strip
price = nike.css("div.product-card__price").text.strip
shoes = self.new
#all << shoes #having error here
end
end
Hope this helps you

How to access element in array via command line in ruby?

Given a simple code in file test.rb:
def todo_list(todo_selector)
library = ["Get a cat", "Get a dog", "Build a fighting ring"]
puts "Your current step in todo-list is:\n#{library[todo_selector]}"
end
ARGV.each { |todo| todo_list(todo_selector) }
How am I able to call this method with an index via command line?
Normally I would use test.rb 1, but I am getting this error:
Traceback (most recent call last):
2: from test.rb:17:in `<main>'
1: from test.rb:17:in `each'
test.rb:17:in `block in <main>': undefined local variable or method `todo_selector' for main:Object (NameError)
Did you mean? todo_list
What am I doing wrong?
Try this way for one element, take care to convert String into Integer in order to access the Array by index.
selector = ARGV[0].to_i
todo_list(selector)
For an array of arguments: ARGV.each{ |i| todo_list(i.to_i) }

ERROR -- : undefined method `each' for nil:NilClass (NoMethodError)

I am getting the above error when I try to run /update_project_permissions.rb script of Rally-User-Management.
update_project_permissions.rb
#include for rally json library gem
require 'rally_api'
require 'csv'
require './lib/multi_io.rb'
require './lib/rally_user_helper.rb'
require './lib/go_update_project_permissions.rb'
$project_identifier_arg = ARGV[0]
$new_permission_arg = ARGV[1]
if $project_identifier_arg.nil? || $new_permission_arg.nil? then
puts "Usage: ruby update_all_project_permissions.rb \"My Project\" \"Editor\""
puts "or: ruby update_all_project_permissions 12345678910 \"No Access\""
puts "Where in number form, the project identifier is the Project's ObjectID."
end
begin
go_update_project_permissions($project_identifier_arg, $new_permission_arg)
end
which calls file
lib/go_update_project_permissions.rb at
go_update_project_permissions
go_update_project_permissions
The command line argument is - ruby update_project_permissions.rb "40880544785" "No Access"
I am getting the following error
ERROR -- : undefined method `each' for nil:NilClass (NoMethodError)
c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:92:in `is_workspace_admin'
c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:137:in `update_project_permissions'
c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:309:in `block in go_update_project_permissions'
c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:308:in `each'
c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:308:in `go_update_project_permissions'
update_project_permissions.rb:38:in
[2015-08-10T15:38:28.854098 #15504] ERROR -- :
["c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:92:in `is_workspace_admin'", "
c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:137:in `update_project_permissions'", "
c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:309:in `block in go_update_project_permissions'", "
c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:308:in `each'", "
c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:308:in `go_update_project_permissions'", "update_project_permissions.rb:38:in "]E,
[2015-08-10T15:38:28.857098 #15504] ERROR -- : undefined method `each' for nil:NilClass
Thanks for finding this bug. There's an updated version here:
https://github.com/markwilliams970/Rally-User-Management
That should address the problem.

Resources