Using Ruby in Mac OS terminal: unterminated string meets end of file - ruby-on-rails

I have been going through a Ruby tutorial on Mac OS X 10.10.1, running Ruby 2.0.0p481. My program is saved as "calc.rb".
When I am asked to run a basic string:
puts 'i like' + 'apples.'
the terminal will return either:
calc.rb:4: syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '('
or:
calc.rb:1: unterminated string meets end of file
This is seemingly at random. I'm unsure how to move forward. I also had this problem for around 30 minutes just trying to run a basic string like:
puts 'hello, world!'

Related

wkhtmltopdf_binary_gem : Syntax error: word unexpected (expecting ")")

In production server (ubuntu 14.04) getting this error while PDF download
referred link and tried
https://gist.github.com/brunogaspar/bd89079245923c04be6b0f92af431c10
$ wkhtmltopdf http://www.google.com google.pdf
/usr/local/rvm/gems/ruby-2.5.1/gems/wkhtmltopdf-binary-0.12.4/bin/wkhtmltopdf_linux_x86: 1: /usr/local/rvm/gems/ruby-2.5.1/gems/wkhtmltopdf-binary-0.12.4/bin/wkhtmltopdf_linux_x86: Syntax error: word unexpected (expecting ")")
any help?

Unable to use backticks to use tr linux command in ruby on rails

tr -d \\000-\\177 < #{file_path} | wc -c
The above command is used to remove ascii characters and check word count.
While running the above command in ruby on rails using backticks, \000-\177 which needs to be considered as numbers range in octal format is being considered as string and is giving wrong results.
The above command works fine while we run on a file in linux terminal.
Some suggest a way to do this.
Have you tried quadruple backslashes to escape your double backslashes? I tested and it works for me (note I narrowed the ASCII match so that I'd get something other than 0):
`tr -d \\\\000-\\\\170 < Rakefile | wc -c`
=> "3\n"
Only 3 characters returned, a bunch of y's from my Rakefile

Are there any Ruby Interpreter/compiler to execute bunch of ruby code

I am looking for a interpreter or compiler tool which can help me to capture syntax errors of application built in ruby code and which should also integrate with Jenkins for CI and CD process.
Standard ruby binary can do that
ruby -c your_ruby_file.rb
Example
% ruby -c test.rb
test.rb:8: syntax error, unexpected keyword_end, expecting end-of-input
For this file
# test.rb
count = 1_000_000_000
iter = 0
while iter <= count
c = 1 + 2
iter = iter + 1
end
end

rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych.rb:205:in `parse': (<unknown>): could not find expected ':' while scanning a simple key at line 18 column 3

This is the full error I am getting when doing a simple:
$ rails generate
Users/localuser/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych.rb:205:in
`parse': (): could not find expected ':' while scanning a
simple key at line 18 column 3(Psych::SyntaxError)
Any ideas whats going on?
My system:
ruby 2.0.0p0 [x86_64-darwin12.2.0] Rails 3.2.13 mysql Ver 14.14
Distrib 5.6.10, for osx10.8 (x86_64)
This an extract of the psych.rb file mentioned in the error
#See Psych::Nodes for more information about YAML AST.
def self.parse_stream yaml, filename = nil, &block
if block_given?
parser = Psych::Parser.new(Handlers::DocumentStream.new(&block))
parser.parse yaml, filename
else
parser = self.parser
parser.parse yaml, filename
parser.handler.root
end
end
This error usually appears if you have a syntax error in your YAML file(s).
I had this error when I had a tab instead of whitespace in the yaml file.
I had this error when the YAML ended in a NULL '\0' character instead of empty whitespace.

Post XML to an external API - Using cURL in Rails

I've been working in the Rails console with Rails 3.2 and I'm generating an XML file using Nokogiri. From here, I need to post to an external API to grab some data and return it within my app. Eventually this code will be a function of the controller, but for now I've been experimenting in the console.
I generated and XML file with Nokogiri and parameters I specified, and I stored the output using the following command:
File.open('results.xml', 'w') {|f| f.write(results)}
From here, I want to POST this file to the external API. The command I used saved it in the /public directory of my app. From here, I'm unsure of how to access it with cURL.
I tried putting it in a views directory and set up a route so I can GET the file, and I can at least access it. Here is what I tried in cURL (note that the Rails server was running at the time and the API path below is made up for example purposes):
curl -X POST -v --data-ascii http://localhost:3000/search/postresults.xml http://APIPATH/example.php
This one has been frustrating me for awhile, and when I try that I get an error saying:
SyntaxError: (irb):5: syntax error, unexpected tCONSTANT, expecting keyword_do or '{' or '('
curl -X POST -v --data-ascii http://local...
^
(irb):5: syntax error, unexpected tUMINUS, expecting keyword_do or '{' or '('
curl -X POST -v --data-ascii http://localhost:...
^
(irb):5: syntax error, unexpected tLABEL, expecting keyword_do or '{' or '('
...l
-X POST -v --data-ascii http://localhost:3000/search/postr...
... ^
(irb):5: unknown regexp options - lcalht
I've tried all of the standard troubleshooting (curl is installed - version 0.0.9, server is running, curl is in my Gemfile, etc), so any help is much appreciated. Thanks!
Your errors suggest that you typed the curl command and arguments directly into IRb. That's not how Ruby gems work. Furthermore, if you want to POST to an HTTP resource from Rails don't bother with cURL. Rails has built-in tools for this.
If you're going to be interacting with this API a lot, and if the API is fairly RESTy, then take a look at ActiveResource (tutorials abound on Google if the docs don't do it for you).
If you're not using a very RESTy API, or if this is a one-off API call, you can create an instance of ActiveResource::Connection directly, e.g.:
conn = ActiveResource::Connection.new 'http://example.com'
result = conn.post 'example.php', results
There's likely no need to write the Nokogiri document (results) to a file, just give it directly to ActiveResource::Connection#post.

Resources