Questions tagged [ruby-block]
37 questions
1
vote
1 answer
Ruby Blocks inside module or class
Can a block in Ruby be written inside class or module? as per docs a block can be called from methods using yield...i.e it should be callable from methods in classes also. But for the below code as I am getting the following error:
$ ruby…

Santhosh Nagulanchi
- 724
- 4
- 12
- 23
1
vote
1 answer
Chef ruby_block ShellOut mysql result stdout forever empty
have an next case:
test3 = Mixlib::ShellOut.new("echo '1'")
test4 = Mixlib::ShellOut.new("mysql -u root --silent --skip-column-names --password='rootpass' -e 'some sql;'")
test3.run_command
test4.run_command
puts test3.stdout # => 1
puts…

Maxim Agrigoroae
- 11
- 2
1
vote
1 answer
Require rest-client to module in Chef
I need to include res-client into my module in order to use RestClient::Resource.
I use the method in the module in a Chef recipe (ruby_block resource).
When my ruby_block resource tries to run the method in the module it output this error:
ERROR:…

Hosein Zarifi
- 59
- 9
1
vote
2 answers
Ruby: passing blocks as parameters from oustide class
I am getting below error when executing code:
example.rb:9:in `': undefined method `each' for main:Object (NoMethodError)
Line 9 is second last line in my code.
My code:
class TargetProvider
def each(target,&block)
block.call(target)
…

ojas
- 2,150
- 5
- 22
- 37
1
vote
1 answer
Pass a ruby &block using rspec
I want to test the functionality of the method using rspec that receives anonymous block and not raise error. Below is my code:
class SP
def speak(options={},&block)
puts "speak called"
block.call()
rescue StandardError => e
puts…

ojas
- 2,150
- 5
- 22
- 37
1
vote
2 answers
using print inside def having yield statement
I am trying to print inside a function.
The function is used for invoking a block.
But I don't see the print happening in the function definition.
Please shed a light on this. Basically I am not clear with the control flow.
def find_all
…

new user20
- 59
- 4
0
votes
1 answer
why pass block arguments to a function in ruby?
I'm unclear on why there is a need to pass block arguments when calling a function.
why not just pass in as function arguments and what happens to the block arguments, how are they passed and used?
m.call(somevalue) {|_k, v| v['abc'] =…

suwa
- 107
- 2
- 10
0
votes
1 answer
Can a Rails helper_method use yield as if it was invoked in the corresponding view?
I have the following accordion generator which works fine when included directly in the view:
<%
def collapser(name)
fad = {
class: 'collapsed',
data: {toggle: 'collapse', parent: '#accordion_id'},
href: "##{name}",
aria:…

psychoslave
- 2,783
- 3
- 27
- 44
0
votes
1 answer
Ruby: how to make variables visible inside methods within a block?
The following code gives me
NameError: undefined local variable or method `dir'
in extract_snapshots method.
The code is intended to extract snapshots from a video, store them in a created temporary directory, send the snapshots to a service and…

mSourire
- 29
- 4
0
votes
1 answer
Unable to escape dollar sign in ruby_block chef resource
I'm writing a chef recipe wherein an sql command is run.
ruby_block 'SQL command' do
block do
report = open(ReportFile,'a')
command = %Q( ssh -o StrictHostKeyChecking=no root@#{dbHost2} 'su - #{oraSidUser} -c "#{oracleHome}/bin/sqlplus /…

Ivan
- 169
- 10
0
votes
0 answers
Chef set node attribute value from a regex matching
I am trying to set node attribute with sub string from a file, but I am not able to achieve that successfully.
Content of #{ENV['TEMP']}\fullname.txt is
Full Name King Arthur
And I want to assign only "King Arthur" to node attribute…

Sridhar
- 803
- 1
- 12
- 21
0
votes
1 answer
Ruby faraday openweathermap api configure block and then conn.get block
require 'faraday'
require 'pry'
ENV['API'] = "XXXXXXXXXXXX"
conn = Faraday.new(:url => 'http://api.openweathermap.org/data/2.5') do|faraday|
faraday.request :url_encoded # form-encode POST params
faraday.response :logger …

bRRRITSCOLD
- 45
- 8
0
votes
1 answer
Ruby - Apply method on all block variables
Having for example
sum = 0
2.times do |v1, v2, v3 , v4|
v1 = FactoryGirl...
v2 = FactoryGirl...
..
v4 = ...
sum =
end
Now on sum I would like to add the value of an attribute that each object from the block has it eg
sum =…

beginnersquestions
- 63
- 6
0
votes
1 answer
Ruby block - return yield running code after yield
I want to return the output of yield but also execute the code after yield, is there a more "right" way?:
def myblock
yield_output = yield
puts 'after yield'
yield_output
end
myblock {'my yield'}
# after yield
# => my yield

joshweir
- 5,427
- 3
- 39
- 59
0
votes
3 answers
Ruby method call using round brackets throwing syntax error
I'm having some trouble understanding why I can't call select with round brackets.
a = [1,2,3]
a.select {|v| v.is_a?(Integer)} # works
a.select({|v| v.is_a?(Integer)}) # syntax error, unexpected '|', expecting '}
As far as I can tell select is a…

ConorSheehan1
- 1,640
- 1
- 18
- 30