Ruby syntax error, unexpected tSTRING_BEG, expecting ':' (SyntaxError) [closed] - ruby

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am running a Ruby script but getting the below error message.
root#li140-48:~/rbircd-master# ruby ircd.rb
ircd.rb:38:in `load': ircclient.rb:145: syntax error, unexpected tSTRING_BEG, expecting ':' (SyntaxError)
(value == true) ? key.upcase"#{key.upcase}=#{value}"
^
from ircd.rb:38:in `reload!'
from ircd.rb:41:in `<main>'
root#li140-48:~/rbircd-master#
Here is the code block around ircclient.rb:145:
def send_version(detailed=false)
if detailed
send_numeric 351, 'RubyIRCd0.1.0.', #server.name, 'FhiXeOoZE [Linux box 2.6.18-128.1.1.el5.028stab062.3 #1 SMP Sun May 10 18:54:51 MSD 2009 i686=2309]'
send #server.name, :notice, #nick, 'OpenSSL 0.9.8k 25 Mar 2009'
send #server.name, :notice, #nick, 'zlib 1.2.3'
send #server.name, :notice, #nick, 'libcurl/7.19.4 GnuTLS/2.6.6 zlib/1.2.3 c-ares/1.6.0 libssh2/0.18'
end
features = ServerConfig.features.clone
features.each_slice(13) do |slice|
slice.map! do |(key, value)|
(value == true) ? key.upcase"#{key.upcase}=#{value}"
end
slice << 'are supported by this server'
send_numeric '005', *slice
end
end
Please can someone help me?

You're doing a ternary if but you're missing the else clause.
Change
(value == true) ? key.upcase"#{key.upcase}=#{value}"
to
value ? key.upcase : "#{key.upcase}=#{value}"
And then read How do I use the conditional operator in Ruby

Related

Ruby will not return hash from function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have the following code:
def parse_package_url package, directory
branchget = package.split '#'
branch = branchget.length > 1 ? branchget[1] : false
siteget = branchget[0].split(':')
site = siteget.length > 1 ? siteget[0] : 'gitlab'
repoget = (siteget.length > 1 ? siteget[1] : siteget[0]).split '/'
packagename = repoget[1]
packageuser = repoget[0]
path = "#{directory}/#{packagename}"
{
:branch => branch,
:site => site,
:name => packagename,
:user => packageuser
:path => path,
:repo => repoget.join('/')
}
end
Upon running this code, I get the following errors:
syntax error, unexpected tSYMBEG, expecting '}' (SyntaxError)
:path => path,
^
syntax error, unexpected tASSOC, expecting tCOLON2 or '[' or '.'
:repo => repoget.join('/')
syntax error, unexpected '}', expecting keyword_end
I cannot spot my syntax mistake in this code, and would appreciate if somebody could point it out.
You're missing the comma after :user => packageuser which is causing a syntax error. With errors like these, years of staring have taught me the issue is often with one line above the line number Ruby gives you.

Lambda returning different values [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm struggling with the following code:
I want a method to check if a string has content or not.
has_content = -> (a) { a!=nil && a.strip != ''}
c = ' '
has_content.call(c)
=> false
c.has_content
=> true
Why is the response different? Clearly I am lacking some Proc/lambdas knowledge.
I believe there is something missing in that code that is causing such behavior.
has_content is not defined for String, so unless you defined it before, it should raise an error
1.9.3p429 :002 > ''.has_content
NoMethodError: undefined method `has_content' for "":String
from (irb):2
from /Users/weppos/.rvm/rubies/ruby-1.9.3-p429/bin/irb:12:in `<main>'
As a side note, here's an alternative version of your code
has_content = ->(a) { !a.to_s.strip.empty? }
And here's an example
has_content.(nil)
# => false
has_content.('')
# => false
has_content.(' ')
# => false
has_content.('hello')
# => true

Argument should be a vector [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I'm trying to use the statsample library, but having issues with arrays/vectors.
b = [2,3,4,5,6,7].to_scale
# => #<TypeError: Argument should be a Vector>
Do you know why I might be getting this error?
EDIT 1
Something odd is going on in my environment....
$ irb
irb(main):001:0> require 'statsample'
=> true
irb(main):004:0> b = [2,3,4,5,6,7].to_scale
=> Vector(type:scale, n:6)[2,3,4,5,6,7]
exit
$ bundle exec irb
irb(main):001:0> b = [2,3,4,5,6,7].to_scale
NoMethodError: undefined method `to_scale' for [2, 3, 4, 5, 6, 7]:Array
from (irb):1
from /Users/brandon/.rbenv/versions/1.9.3-p484/bin/irb:12:in `<main>'
irb(main):002:0>
For some reason statsample is not being required when I use bundle exec. I have to manually require 'statsample in my code, even though gem 'statsample is in my Gemfile.
Any thoughts??
I don't see the issue:
irb(main):004:0> require 'statsample'
=> true
irb(main):004:0> b = [2,3,4,5,6,7].to_scale
=> Vector(type:scale, n:6)[2,3,4,5,6,7]
Please make sure that if you use the bundler, put into the Gemfile the following:
gem 'statsample'
And execute the bundle install.
According to the source code:
module Statsample::VectorShorthands
# Creates a new Statsample::Vector object
# Argument should be equal to Vector.new
def to_vector(*args)
Statsample::Vector.new(self,*args)
end
# Creates a new Statsample::Vector object of type :scale
def to_scale(*args)
Statsample::Vector.new(self, :scale, *args)
end
end
class Array
include Statsample::VectorShorthands
end
So here my guess is:
If it's just [Array].to_scale, it should have no problem at all. Unless you pass any argument to to_scale() which is not Vector type, because inside it's calling Statsample::Vector.new(self, :scale, *args), and it's saying "Argument should be equal to Vector.new".

Code not working after little change [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm beginner in programming, I changed my code a little bit so that I can execute some def from the command line. After that change I got an error, but first my code:
require 'faraday'
#conn = Faraday.new 'https://zombost.de:8673/rest', :ssl => {:verify => false}
#uid = '8978'
def certificate
res = conn.get do |request|
request.url "acc/#{#uid}/cere"
end
end
def suchen(input)
#suche = input
#res = #conn.get do |request|
request.url "acc/?search=#{#suche}"
end
end
puts #res.body
Then I wrote into the console:
ruby prog.rb suchen(jumbo)
So, somehow i get the error:
Undefined method body for Nilclass
You're not invoking either of your methods, so #res is never assigned to.
#res evaluates to nil, so you're invoking nil.body.
RE: Your update:
ruby prog.rb suchen(jumbo)
That isn't how you invoke a method. You have to call it from within the source file. All you're doing is passing an argument to your script, which will be a simple string available in the ARGV array.
RE: Your comment:
It should go without saying that the solution is to actually invoke your method.
You can call the methods from the command line. Ruby has a function eval which evaluates a string. You can eval the command line argument strings.
Here's how. Change the line puts #res.body to
str = ARGV[1]
eval(ARGV[0])
puts #res.body
then run your program like so
$ ruby prog.rb suchen\(str\) jumbo

syntax error unexpected tIDENTIFIER [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I get the error message
syntax error, unexpected tIDENTIFIER, expecting ')'
leg = legislators_by_zipcode(zipcode)
when executing the code below:
require 'csv'
require 'sunlight/congress'
Sunlight::Congress.api_key = "c..."
def clean_zipcode(x)
x = x.to_s.rjust(5, "0")[0..4]
end
def legislators_by_zipcode(zipcode)
legislators = Sunlight::Congress::Legislator.by_zipcode(zipcode)
legislator_names = legislators.collect do |legislator|
"#{legislator.first_name} #{legislator.last_name}"
end
legislator_names.join(", ")
end
contents = CSV.open "event_attendees.csv", headers: true, header_converters: :symbol
contents.each do |row|
name = row[:first_name]
zipcode = clean_zipcode(row[:zipcode]
leg = legislators_by_zipcode(zipcode)
puts "#{name} #{zipcode} #{leg}"
You missed the closing brace()) here zipcode = clean_zipcode(row[:zipcode] in your code.Re write it as zipcode = clean_zipcode(row[:zipcode]).

Resources