syntax error unexpected tIDENTIFIER [closed] - ruby

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]).

Related

Ruby how to get file from public dir? [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 2 years ago.
Improve this question
def get_xml
path = "ddd-66252.pdf" // in public
way = File.basename(path)
diff = File.read(path)
render :xml => diff
end
How i can get file from path and need to look like file
Assuming you are using Ruby on Rails because you mention a public folder and have a render method in your code. In Ruby on Rails you can use send_file in your controller like this to send files to the browser:
def send_pdf
send_file(
Rails.root.join('public', 'ddd-66252.pdf'),
filename: 'ddd-66252.pdf',
type: 'application/pdf'
)
end

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.

Ruby syntax error, unexpected tSTRING_BEG, expecting ':' (SyntaxError) [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 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

Ruby: remove from the string [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 have the URL http://localhost:3000/en/invest_offers?utf8=blahblahblah. How can I get ?utf8=blahblahblah part of the URL? Thanks!
Do as below using URI::Generic.query:
require 'uri'
URI("http://localhost:3000/en/invest_offers?utf8=blahblahblah").query
# => "utf8=blahblahblah"
Use URI::parse, URI::Generic#query:
require 'uri'
url = 'http://localhost:3000/en/invest_offers?utf8=blahblahblah'
URI.parse(url)
# => #<URI::HTTP:0x0000000213aae0 URL:http://localhost:3000/en/invest_offers?utf8=blahblahblah>
URI.parse(url).query
# => "utf8=blahblahblah"
Do the following:--
require 'uri'
url = 'http://localhost:3000/en/invest_offers?utf8=blahblahblah'
parsed_url = URI.parse(url)
if you just want to get string query part like "utf8=blahblahblah"
you should do
parsed_url.query
if you want get "blahblahblah",
you should just do
params["utf8"]
or else
p = CGI.parse(parsed_url.query)
# p is now {"utf8"=>["blahblahblah"]}
p["utf8"].first
#=> "blahblahblah"

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

Resources