Ruby - Error: "unexpected keyword_end, expecting $end" - ruby

First off, I'm new to ruby so I apologize if I did something that's not conventional. I am just trying to run a simple script that interacts with twitter. It works perfectly fine on my macbook but when I try to run it on my raspberry pi I get this error: "unexpected keyword_end, expecting $end." I found posts involving this error, but I didn't feel like the answers helped me.
It's mainly just throwing me off because it's not happening on my mac.
This is the script (or at least a portion of it) I'm having problems with:
#encoding: UTF-8
require 'tweetstream'
require 'rubygems'
require 'oauth'
require 'json'
puts TweetStream::VERSION
c_key = 'xxxx'
c_secret = 'xxxx'
oa_token = 'xxxx'
oa_token_secret = 'xxxx'
TweetStream.configure do |config|
config.consumer_key = c_key
config.consumer_secret = c_secret
config.oauth_token = oa_token
config.oauth_token_secret = oa_token_secret
config.auth_method = :oauth
end
consumer_key = OAuth::Consumer.new( c_key, c_secret)
access_token = OAuth::Token.new( oa_token, oa_token_secret)
client = TweetStream::Client.new
keywords = ['word','word']
client.userstream do |status|
if keywords.any? {|str| status.text.downcase.include? str} && status.user.id.to_s != '11111111111'
unless status.text[0,4].include? 'RT #'
puts "#{status.id}: #{status.text}\n"
end
end
end
Any help or guidance would be much appreciated!
Okay so I just copied over my code from my other computer again and it's working fine. I have no clue what happened because I never really fiddled with it when I brought it over the first time. Thanks for the help though!

How to find elusive syntax errors in Ruby
I'm not going to tell you where your syntax error is, but I'll tell you how to find out.
Start commenting out code. This is easiest done with an editor that allows you to highlight a block of code and comment it out with one command (for example, in Emacs, c-C c-C toggles whether or not the highlighted block is commented-out). Start with the part of your code where you most recently made a modification (if you're not sure, check your version control system to see what you've changed). Pick some part of it--perhaps half, to comment out. Be sure that you're commenting out belongs together--for example, don't comment out half of a while loop--comment out the whole thing.
Now load your program. Do you still get the syntax error? If not, you've just identified where it must be. Take some of the code you commented out, and un-comment it out. If so, find another block of code and comment it out.
Lather, rinse repeat.
You don't have to worry about whether your program runs -- if it loads without a syntax error, it will no doubt fail because of all the commented out code. All you're doing is finding out where the syntax error is.

Funny story....I had the same error before and it was driving me NUTS for like an hour. No kidding. I refused to give up. I just could not figure it out! Why the error? My code was on-point.
And then....I happen to hit the scroll-down bar only to see that there was already an "end" inserted below like 2 lines down!! I couldn't see it on the screen. There was an extra!!! Smh...
Once I took it out...my "bundle exec rake test" passed. Sometimes it's the little things :)

change
unless status.text[0,4].include? 'RT #'
puts "#{status.id}: #{status.text}\n"
end
to:
if !status.text[0,4].include? 'RT #'
puts "#{status.id}: #{status.text}\n"
end
or:
puts "#{status.id}: #{status.text}\n" unless status.text[0,4].include? 'RT #'
unless something is equivalent to if !something
unless does not need an end for it.

Related

Ruby: 'no implicit conversion of nil into String(TypeError) from Zed Shaw's LRTHW

Just beginning to learn programming, and starting out with Ruby. Doing some copy typing exercises from Zed Shaw's Learn Ruby the Hard Way
In doing exercise 15 and 16, covering opening files, I run into the same problem when I try to run it.
target = open(filename, 'w')
I get the message:
say.rb:10:in open': no implicit conversion of nil into String
(TypeError) from say.rb:10:in'
What does this mean? And how do I correct this?
Thanks in advance!
Direct screen shot from the LRTHW website
I think your filename variable value is not initialized or nil.
filename = "path/to/file"
target = open(filename, 'w') if filename.present?
Everyone: thanks for peering into my question. I had a friend of mine figure out what the problem was. I had forgotten that the whole point of the exercise was to use the ARGV, and that was the missing link, and thus the error. The first line of code is: filename = ARGV.first. When I was running this on the Terminal, I ran it as >ruby say.rb, without submitting a filename afterwards. So, on the prompt, I should have submitted it as >ruby say.rb whateverfilename.doc. So, without that, the program had nothing to chew on. Thanks again!

Why backspace key deletes the entire row in JRuby and what can be the fix for it?

I have developed a couple of tools in JRuby. Both the tools prompt for few questions to the user. While answering these questions if the user wants to correct a wrong entry and hits backspace key that deletes the entire line (including the question) instead of deleting just one character.
Below is the piece of code that prompts the questions:
require highline/import
def prompt
begin
input=ask("\t\tEnter user name: ") do |ch|
ch=true
end
input
rescue => e
print "Error -"+e
end
end
I was wondering if anyone of you have seen this kind of problem before and what can be the fix for this? Really appreciate your time and help.
Thanks in advance.
Below is the working piece of the code. I removed \t and changed ch=true to ch.readline=true. Thank for all of your help and guidance.
require highline/import
def prompt
begin
input=ask("Enter user name: ") do |ch|
ch.readline=true
end
input
rescue => e
print "Error -"+e
end
end
If you are using highline gem, then when reading the input instead of
input=ask("Enter the Input: "){|ch| ch.echo = true}
use
input=ask("Enter the Input: ") do |ch|
ch.readline=true
end
Source: https://www.ruby-forum.com/topic/4406162

puts line breaking in_branch in git gem?

I'm automating a bit of git workflow and have found some curious behaviour when using the git gem's in_branch method and wondered if anyone could explain why or how this issue occurs? Here's some test code that should reproduce the issue:
#!/usr/bin/env ruby
require 'git'
# git details
repo_name = 'SOME_REPO'
working_dir = "#SOME_DIR/git_test_repo"
repo_owner = 'SOME_GIT_USER'
repo_host = 'SOME_GIT_HOST'
repo_dir = "#{working_dir}/#{repo_name}"
remote_repo = "git##{repo_host}:#{repo_owner}/#{repo_name}.git"
branch_name = 'testbranch'
commit_message = 'log line breaking in_branch test'
Dir.mkdir(working_dir) unless Dir.exist?(working_dir)
Dir.chdir(working_dir)
Git.clone(remote_repo, repo_name) unless Dir.exists?(repo_dir)
repo = Git.open(repo_dir)
repo.pull(remote = 'origin', branch = 'master')
repo.branch(branch_name).in_branch(message = commit_message) do
File.write(repo_dir + '/test.txt', Time.now)
repo.add('.')
# -----this line breaks it --------------
puts 'committing changes'
# ---------------------------------------
end
When this code runs, the last puts line before the end of the in_branch block, when actually run, somehow causes the changes in the branch to be reverted, but when it's commented out, all the code behaves as expected. I've tested output lines anywhere in the block, and they all behave fine. It seems to happen across many versions of ruby (custom installs, rvm installs) and different OS's (linuxes and mac).
Is there some arcane behaviour of ruby and its terminal output I need to be aware of here?
Not really "arcane behavior of ruby". But the result of the last line in a method is what the method returns. Putting a puts in that position will usually break the method.
Just testing and I see the same behavior using print (which does not add a newline).
This is not a newline issue, moving the (now print) line above the repo.add line and all works.
Anymore thoughts?

Cucumber no output screen

I'm pretty new to cucumber automated testing. When I first started, a co-worker had set up everything for me on my computer, and (now that he's gone) I've run into a problem that I can't seem to figure out.
I'm using cucumber to test a web application. In the past when I run the script, an internet explorer pops up, and I can see each line of the script being executed.
I recently had to reinstall cucumber, ruby, watir, etc., and that internet explorer screen no longer pops up.
I installed Ruby 1.9.3, cucumber (gem install cucumber), watir (gem install watir). Am I missing something? Is it an extra plug in? The script still runs. However, instead of taking say 1 min + to run a 320 step script, it now takes 1.5 seconds. There are no error messages. When run from the command window, it literally looked like it just scrolled through the script instead of going through each step.
What is the pop up screen called anyways? A scenario screen? Output screen?
It was really difficult for me to look it up on google because I had no idea how to refer to that screen.
Any help is appreciated. and I realize I might not have described the problem well enough. Just leave a comment, and I can try to clarify it more.
Feature:
'To go to a webpage'
Scenario:
# ----------
# GO TO PAGE
# ----------
Given that I have gone to the Login page at "url"
#
# ----------
# LOG IN
# ----------
When I add "username" to the Username
When I add "password" to the Password
And click the Login button
Then "Welcome" should be mentioned on the page
script definitions:
require "rubygems"
require "watir"
puts "Browser is running..."
END {
puts "Closing browser..."
}
BEGIN {
puts "Starting browser..."
}
Given /^that I have gone to the Login page at "(.*)"$/ do |item|
#browser = Watir::IE.start(item)
lnk_found = 0
#browser.links.each do |lnk|
if lnk.id.to_s.matches("overridelink")
lnk_found += 1
end
end
if lnk_found > 0
#browser.link(:id, "overridelink").click
end
# puts "Watir Version: #{Watir::IE::VERSION}"
#browser.maximize
end
#
#
#
When /^I add "(.*)" to the Username$/ do |item|
#browser.text_field(:name, "loginName").set(item)
end
#
#
When /^I add "(.*)" to the Password$/ do |item|
#browser.text_field(:name, "passwd").set(item)
end
#
#
#
Then /^"(.*)" should be mentioned on the page$/ do |item|
if #browser.text.include?(item)
# puts "TEST PASSED. FOUND >#{item}<"
else
puts "*** TEST FAILED ***. >#{item}< was not found."
end
end
Directory Structure
Cucumber
Testing
lib
login.rb
login.feature
I don't know what exactly fixed it but after taking suggestions and advices from different people I basically uninstalled and reinstalled watir several times.
I also updated the code, took out "require 'rubygems'" and replaced Watir::IE.start with Watir::Browser.start.
It seems like the problem was the existing scripts I worked with was based off of older versions of watir/cucumber/ruby, so when I had to reinstall everything, the scripts were no longer compatible with newer versions of everything.

Geektool and TextMate Error

I am giving myself little projects during the winter break, and am trying to write little geektool scripts I can use for my desktop. I wrote this for ruby and it works in terminal, but when I run it in textmate I get a host is down error, and when I place it in geektool it will not run.
#!/usr/bin/ruby
require 'open-uri'
require 'nokogiri'
def fetch_xml()
open("http://weather.yahooapis.com/forecastrss?w=2424766&u=f")
end
def parse_xml()
source = Nokogiri::XML(fetch_xml)
location = source.xpath("//yweather:location")
condition = source.xpath("//item//yweather:condition")
forecast = source.xpath("//item//yweather:forecast")
[location[0]['city'], location[0]['region'], condition[0]['temp'].to_i, condition[0]['text'], forecast[0]['high'], forecast[0]['low']]
end
def display_weather()
result = parse_xml()
print "#{result}\n"
end
display_weather
It runs in terminal and gives me the correct output: ["Houston", "TX", 64, "Cloudy", "69", "63"]
But like I mentioned earlier, it will not run within textmate, and nothing shows up in geektool. Thanks in advance for any help.

Resources