Ruby Block Syntax Error
Try this:
version(:full) { process(:resize_to_limit => [960, 960]) }
version(:half) { process(:resize_to_limit => [470, 470]) }
version(:third) { process(:resize_to_limit => [306, 306]) }
version(:fourth) { process(:resize_to_limit => [176, 176]) }
You have a precedence problem. The { }
block binds tighter than a do...end
block and tighter than a method call; the result is that Ruby thinks you're trying to supply a block as an argument to a symbol and says no.
You can see a clearer (?) or possibly more familar example by comparing the following:
[1, 2, 3].inject 0 { |x, y| x + y }
[1, 2, 3].inject(0) { |x, y| x + y }
Ruby Syntax error when passing block and array to function
The block shouldn't be passed as a parameter to the method 'test'.
The following will work:
test([0,1,2,3]) {puts 'string'}
Alternatively, you can create a proc (which is an object unlike a block) and pass it to the 'test' method.
p = Proc.new {puts 'string'}
test([0,1,2,3], &p)
Ruby curly bracket block syntax is not working while do...end works
It's a parsing/precedence issue. Braces try to bind to the nearest token, which is :day
in this case, but you want it to bind to every()
. You have to write every(:day) { rake 'billing:daily' }
to explicitly bind it to the correct token.
why does ruby throw syntax error when attempting an yield within a block?
Here's the cleaned up version:
def test
x = -> { p 'l1' }
yield -> { p 'yield2' }
end
Note that yield
is a keyword, not a method call, so you need to be more explicit. You can't just slap a block on there and call it done, it has to be a proper lambda
.
Then in your call, you can't break out the block, it's just a regular argument:
test {|b| p 'in yield1'; b.call }
Now it works.
Does anyone know why IRB gives the syntax error in block?
Because counter++
is not a valid ruby expression. You should replace it with counter += 1
.
Ruby syntax error when try to puts literal hash
Ruby has a glitch because it's too permissive with parentheses. You can leave them off nearly anything, but...
puts{}
parses as "call puts
with a block {}
." A block, in turn, must contain complete statements, not a comma ,
separated list. So you get the wrong syntax error.
The fix is just puts({})
Next, puts ()
parses as "puts
followed by a single argument, in parentheses." So, again, the parser cannot deal with comma ,
inside the parenthesis. The fix is to take out the space: puts()
Ruby syntax method raises an error, but .push method not when using yield. Why?
While you expect that Ruby interprets acc << yield my_arr[c]
as
acc.<<(yield(my_arr[c]))
Ruby actually understands it like this
acc.<<(yield)(my_arr[c])
Which doesn't make much sense. It can be fixed by using parentheses as others already mentioned:
acc << yield(my_arr[c])
Stack trace showing ERB syntax error. Is the problem with app code or ERB library?
The stack trace shows lines from ERb because ERb code is the one that is executing.
While the issue could theoretically be in either ERb itself or your template, it is much more likely that the issue is in your template given that ERb is used by thousands of developers and applications daily.
Why does `each do` cause a syntax error inside of an annotated method definition?
It appears that on older versions of Ruby, there is a precedence issue with do
/end
blocks.
You can get around that by assigning the return value of the def
expression to a variable, and then passing that variable to the call to Module#private
:
var = def not_ok
collection.each do |x|
puts x
end
end
private var
Note, however, that on older versions of Ruby, the return value of a method definition expression is implementation-defined, so you have no idea what the above code will do. MRI, YARV, MacRuby, MagLev, JRuby, and IronRuby will return nil
as the result of a def
expression, which will raise
a TypeError
, since Module#private
only takes Symbol
s or String
s. Rubinius will return a CompiledMethod
object representing the compiled code for the method, but again, that will raise
a TypeError
from Module#private
.
Only on recent versions of Ruby will a def
expression evaluate to a Symbol
, however, on those recent versions of Ruby, the precedence issue has been fixed as well, so that your code will just work.
It works on YARV 2.2.0 and JRuby-9.0.0.0-dev, but doesn't work on JRuby 1.7.18 and Rubinius 2.2.0 (that last one is probably a bug, since it claims to be compatible with Ruby 2.1.0).
Related Topics
How to Get a Single Character Without Pressing Enter
When to Use 'Self.Foo' Instead of 'Foo' in Ruby Methods
How to Temporarily Redirect Stderr in Ruby
How to 'Validate' on Destroy in Rails
How to Convert a String or Integer to Binary in Ruby
Measure the Distance Between Two Strings With Ruby
Is There Any Difference Between the ':Key =≫ "Value"' and 'Key: "Value"' Hash Notations
What's Different Between Each and Collect Method in Ruby
String Concatenation Vs. Interpolation in Ruby
Find Indices of Elements That Match a Given Condition
Understanding the "||" or Operator in If Conditionals in Ruby
Rails: Activerecord Query Based on Association Value
Why Is the Shovel Operator (≪≪) Preferred Over Plus-Equals (+=) When Building a String in Ruby