Use Sinatra redirect in ternary operator - ruby

Does any one know why
saved ? redirect '/success' : erb :signup
Throws the following error:
syntax error, unexpected ':', expecting keyword_end
While this does not?
saved ? redirect('/success') : erb(:signup)

Related

Rspec returns syntax error of "unexpected '{', expecting keyword_end"

This works on one machine with Ruby but not another.
Code:
describe 'testing reverse string different ways' do
let :thing {'cba321'}
it 'the system method' do
source = '123abc'
result = source.reverse
expect(result).to eq 'cba321'
end
end
Error:
SyntaxError:
/home/michael/Dropbox/90_2019/work/code/ruby__rails/ruby/reverse_string_tests_timing/test_spec.rb:12: syntax error, une
xpected '{', expecting keyword_end
let :thing {'cba321'}
^
/home/michael/Dropbox/90_2019/work/code/ruby__rails/ruby/reverse_string_tests_timing/test_spec.rb:12: syntax error, une
xpected '}', expecting end-of-input
let :thing {'cba321'}
In Ruby 2.4.1 not having parens in the let was allowed but in Ruby 2.5.1 it is not.
So the fix is to add parens to the let, e.g.
change
let :source {'cba321'}
to
let (:source) {'cba321'}

syntax error, unexpected ')', expecting '='

I'm writing an extension for Redcarpet for a Jekyll-powered website. I want to use {x|y} as a tag in markdown that evaluates to the HTML <ruby> tag (and its associates). I wrote this class as per Jekyll's guide, Redcarpet's guide, and this guide on how to do so:
class Jekyll::Converters::Markdown::HotelDown < Redcarpet::Render::HTML
def preprocess(doc)
s = "<ruby><rb>\\1</rb><rp>(</rp><rt>\\2</rt><rp>)</rp></ruby>"
doc.gs­ub!(/\[([\­s\S]+)\|([­\s\S]+)\]/­, s)
doc
end
end
But, I seem to be getting a couple errors when I run bundle exec jekyll serve:
Configuration file: C:/Users/Alex/OneDrive/codes/hotelc.me/hotelc.me/_config.yml
plugin_manager.rb:58:in `require': HotelDown.rb:4: syntax error, unexpected tIDENTIFIER, expecting ')' (SyntaxError)
doc.gs-ub!(/\[([\-s\S]+)\|([-\s\S]+)\]/-, s)
^
HotelDown.rb:4: syntax error, unexpected ')', expecting '='
doc.gs-ub!(/\[([\-s\S]+)\|([-\s\S]+)\]/-, s)
^
It seems there's something wrong with my syntax (an extra space, missing parentheses, or something like that). Is there something I've missed?
Your code has some special characters which is causing this error:
syntax error, unexpected ')', expecting '='
doc.gs-ub!(/\[([\-s\S]+)\|([-\s\S]+)\]/-, s)
Replace your current code with this piece of code:
class Jekyll::Converters::Markdown::HotelDown < Redcarpet::Render::HTML
#Overriding the preprocess() function
def preprocess(doc)
s = "<ruby><rb>\\1</rb><rp>(</rp><rt>\\2</rt><rp>)</rp></ruby>"
doc.gsub!(/\[([\s\S]+)\|([\s\S]+)\]/, s)
doc
end
end
markdown = Redcarpet::Markdown.new(HotelDown)
and it should work!

Syntax error, unexpected ':', expecting ')'

I'm running into this syntax error with the below code and I can't figure out why ruby is complaining about it.
def user_list
server = Lumberg::Whm::Server.new(
host: "localhost",
hash: IO.read("/root/.accesshash")
)
results = server.account.list
accounts = result[:params][:acct].map {|a| a["user"] }
end
end
Syntax error is as follows:
# bundle exec bin/userscan
bin/userscan:3:in `require': /usr/src/userscan/lib/userscan.rb:131: syntax error, unexpected ':', expecting ')' (SyntaxError)
host: "localhost",
^
/usr/src/userscan/lib/userscan.rb:131: syntax error, unexpected ',', expecting kEND
/usr/src/userscan/lib/userscan.rb:133: syntax error, unexpected ')', expecting kEND
from bin/userscan:3
From what I know, the part it's complaining about -should- be okay. Obviously, the semi-colon is actually supposed to be there and the parenthesis should encompass the entirety of the two lines. I've played around with it a bit, but I just keep making it worse rather than better.
Any assistance with what I'm messing up here would be appreciated.
the syntax host: ".." is new to ruby 1.9. If you are using ruby 1.8, you must use the old syntax:
server = Lumberg::Whm::Server.new(
:host => "localhost",
:hash => IO.read("/root/.accesshash") )

Rails 3.2 with ruby 1.8 syntax problems ( unexpected tASSOC, expecting '}', unexpected ':', expecting kEND)

I am trying to deploy a rails 3.2 app where the ruby version is 1.8. I could workaround a few hash syntax troubles I had, but there is still one I can't get over with:
Note I hired a hosting service that won't install ruby 1.9.
The error is
from {app_path}/config/environment.rb:5
[ pid=586526 thr=203092280 file=utils.rb:176 time=2012-05-04 15:32:29.667 ]: *** Exception SyntaxError in PhusionPassenger::Rack::ApplicationSpawner ({app_path}/config/initializers/wrap_parameters.rb:8: syntax error, unexpected tASSOC, expecting '}'
{app_path}/config/initializers/wrap_parameters.rb:8: warning: don't put space before argument parentheses
{app_path}/config/initializers/wrap_parameters.rb:8: warning: don't put space before argument parentheses
from {app_path}/config/environment.rb:5
[ pid=539635 thr=202883380 file=utils.rb:176 time=2012-05-04 14:30:21.570 ]: *** Exception SyntaxError in PhusionPassenger::Rack::ApplicationSpawner ({app_path}/config/initializers/wrap_parameters.rb:8: syntax error, unexpected ':', expecting kEND
from {app_path}/config/environment.rb:5
[ pid=539635 thr=202883380 file=utils.rb:176 time=2012-05-04 14:29:31.744 ]: *** Exception SyntaxError in PhusionPassenger::Rack::ApplicationSpawner ({app_path}/config/initializers/wrap_parameters.rb:8: syntax error, unexpected ':', expecting kEND
The file is config/initializers/wrap_parameters.rb and the content is like follows:
ActiveSupport.on_load(:action_controller) do
# it was originally
# wrap_parameters format: [:json] # ruby 1.9 syntax
# the follow line is line 8
wrap_parameters :format => [:json] # ruby 1.8 syntax
# i already tried
# wrap_parameters {:format => [:json]}
# wrap_parameters({:format => [:json]})
# wrap_parameters(:format => [:json])
end
# Disable root element in JSON by default.
ActiveSupport.on_load(:active_record) do
self.include_root_in_json = false
end
Here is config/enviroment.rb
#config/environment.rb
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
Flog::Application.initialize!
I am a few hours trying to get rid of this issue.
Thank you in advace

quotes issue (ruby)

any idea how I can pass correct argument to xpath? There must be something about how to use single/double quotes. When I use variable
parser_xpath_identificator = "'//table/tbody[#id=\"threadbits_forum_251\"]/tr'" gives me an incorrect value or
parser_xpath_identificator = "'//table/tbody[#id="threadbits_forum_251"]/tr'" gives me an error syntax error, unexpected tIDENTIFIER, expecting $end
require 'rubygems'
require 'mechanize'
parser_xpath_identificator = "'//table/tbody[#id=\"threadbits_forum_251\"]/tr'"
# parser_xpath_identificator = "'//table/tbody[#id="threadbits_forum_251"]/tr'"
#gives an error: syntax error, unexpected tIDENTIFIER, expecting $end
agent = WWW::Mechanize.new
page = agent.get("http://www.vbulletin.org/forum/index.php")
page = page.link_with(:text=>'vB4 General Discussions').click
puts "Page title: #{page.title}"
puts "\nfrom variable: #{page.parser.xpath(parser_xpath_identificator).length}"
puts "directly: #{page.parser.xpath('//table/tbody[#id="threadbits_forum_251"]/tr').length}"
In both cases you're nesting single-quotes directly inside double-quotes, which I don't think is correct. Try this:
parser_xpath_identificator = '//table/tbody[#id="threadbits_forum_251"]/tr'

Resources