OK SO I Am just picking Ruby up pretty much for the kicks and giggles... and Believe me when I say I'm stumped.
I want to create a bot for my Twitch stream and do it in Ruby because I found a fairly easy tut to follow along with, along with my reasoning skills. However I'm having a very hard time getting my command prompt or pry to load the file.
Here is my file JUST IN CASE
require 'socket'
TWITCH_HOST = "irc.twitch.tv"
TWITCH_PORT = 6667
class Fox
def initialize
#nickname = "mybotsname"
#password = "I have the proper oauth here"
#channel = "mytwitchchannel"
#socket = TCPSocket.open(TWITCH_HOST, TWITCH_PORT)
write_to_system "PASS #{#password}"
write_to_system "NICK #{#nickname}"
write_to_system "USER #{#nickname} 0 * #{#nickname}"
write_to_system "JOIN ##{#Channel}"
end
def write_to_system(message)
#socket.puts message
end
def write_to_chat(message)
write_to_system "PRIVMSG ##{#channel} :{message}"
end
end
Now, From what I gathered, I should beable to go into my command prompt and type pry
I get this.
Pry
Now, I want to run my program which is located in a dropbox (Private use)
I'm Still very new to the concept of Repl's as I've been working with Java mostly along with very LITTLE Experience in other languages. What am I doing wrong here? Why can I not get my file to load properly? I've also tried filepathing and got this.FilePathing
I'm sorry if this is a stupid question. It's just driving me absolutely bat-brain crazy. The reason this is driving me bonkers is the video I was watching, he didn't do anything different other than my guess is he was using Terminal instead of Command Prompt. I Wanted originally to do this through Cygwin but upon install of Pry I lost a bunch of Cygwin files and can no longer load Cygwin, I will re-install the over all program later and see what I can from there.
Sorry for no embedded pics.
Also, any easier way to do this I'm all ears. I've tried Komodo Edit 10 but it's not playing nice ether.
Require from LOAD_PATH
A Ruby module or class file needs to be in the LOAD_PATH to require it with Kernel#require. For example, if your file is named just_in_case.rb, you can use:
$LOAD_PATH.unshift '/path/to/dropbox/directory'
# Leave off the path and .rb extension.
require 'just_in_case'
Load from an absolute path
If you need to provide an absolute path, then you should use Kernel#load instead. For example:
# Use the absolute path and the .rb extension.
load '/path/to/dropbox/just_in_case.rb'
Caveats
There are some other differences in behavior between require, require_relative, and load, but they probably don't really matter within the limited scope of the question you asked except that there have historically been issues with Kernel#require_relative within the REPL. It may or may not work as expected now, but I would still recommend require or load for your specific use case.
Related
I am using fastlane and snapshot to create screenshots automatically. To facilitate this I need to copy data into the app. In this case a series of folders and documents from one folder into another within a .app container.
So far the Documents folder is being created but no files are ever copied into it. At this point I’m not sure if I’m suppling the path incorrectly or what else I might be doing wrong to lead to this. It may be simply that I’ve got the wrong code to do that since I’m not that familiar with Ruby.
Any suggestions would be greatly appreciated, so tell me, what am I missing?
example_files = "./sample_data/Documents"
folder_name = "Documents"
setup_for_device_change do |lang, device|
app_path = "/tmp/snapshot/build/SyncSpace.app/"
FileUtils.mkdir_p(File.join(app_path, folder_name))
Dir.glob(File.join(example_files, '*')).each do |example_path|
FileUtils.cp_r(example_path, File.join(app_path, folder_name)) rescue nil # in case the file already exists
end
end
Update -
In the end my problem is that the path I’m using doesn’t work correctly for the files I want to transfer. When I give it a full path from the root level to the folder I need it works. Trying to use a shorter path with just the additional folders over the working directory fails.
So as a follow up, when my working directory is .../fastlane and my data is in .../fastlane/sample_data/Documents then why doesn’t just using ./sample_data/Documents work?
Don't use rescue nil. Ever, or at least until you're very aware of what your code could be doing and why you would want to use that. Instead use the normal form of:
begin
do_something
rescue TheExactExceptionYouWantToHandle => e
# handle the exception
end
That way you handle what you expect and blow up if it's something you didn't expect.
One of my programming mentors years ago told me
Never test for an error condition you don't know how to handle.
On the surface that sounds silly but experience teaches us we can get into situations where the program does the wrong thing trying to do the right thing. It's better to crash fast and safely and spew out an error, than to drag it out and damage things trying to recover or silently cover the error. Debugging a program that's rescuing improperly can be a major pain and a trailing rescue is a common culprit.
Background on what I'm trying to do:
I've been teaching myself Ruby, and as a test project I'm creating a .csv converter. It's working well over all, but what I'm stuck on is allowing the user to easily select the file to be imported. Right now I prompt for the file path with the following code:
def find_original_path
puts "\nWhere is the file that you would like to convert?"
print ">> "
#original_path = gets.chomp
if File.exists?(#original_path)
# Verify that file is a .csv
check_original_type()
else
puts "\nSorry, that file doesn't seem to exist."
# Ask for a new path to file
find_original_path()
end
end
What I'd like to do:
So this works, but it requires that the user know the exact path to their .csv. What I'd prefer is to allow the user to use bash style auto-completion to find their file. Typing ~/ should start off at the user's home directory, and then hitting tab twice should list all available folders/files and so on.
What I've found:
Readline: My understanding is that this module only works for pre-set lists.
Thor: Awesome module, but seems to only work for providing deep argument functionality (e.g. git commands).
Shoes (and variants): Apparently green_shoes in particular has an easy way of doing this, but I'm not yet looking to put a GUI on this tool.
Ruby's Std-lib: I've gone through FileUtils, File, IO, etc...
Backticks and Open4: Makes sense for commands like ls, but not sure about directories.
In Sum:
Haven't found an answer that stood out to me in any of these. I feel like the answer to my problem is either more complex than I realized, or all of my reading has me missing something that's right under my nose. I'm hoping it's the latter, though please be gentle if it's really blindingly obvious :D
Any advice?
I would like to write a Jekyll plugin that makes all posts available in PDF format by utilizing Kramdown's LaTeX export capabilities. For each post in Markdown format, I'd like to end up with the normal .html post along with a .tex file containing the LaTeX markup and finally a .pdf.
Following the documentation for creating plugins, I see two ways of approaching the problem, either with a Converter or with a Generator.
Converter plugins seem to run after the built-in Converters, so the .markdown files have all been converted to .html by the time they reach the Converter.
When I try to implement a Generator, I am able to use fileutils to write a file successfully, but by the end of Jekyll's cycle, that file has been removed. It seems there's a StaticFile class which you can use to register new output files with Jekyll, but I cannot find any real guidance on how to use it.
If you take a look at the ThumbGenerator class in this: https://github.com/matthewowen/jekyll-slideshow/blob/master/_plugins/jekyll_slideshow.rb you'll seen a similar example. This particular plugin makes thumbnail sized versions of all images in the site. Hopefully it gives a useful guide to how you can interact with Jekyll's StaticFile class (though I'm not a Ruby pro, so forgive any poor style).
Unfortunately, there isn't really documentation for this - I gleaned it from reading through the source.
I wrote this a few months ago and don't particularly remember the details (which is why I gave an example rather than a workthrough), but if this doesn't get you on the right track let me know and I'll try to help.
I try to do the same but with direct html->pdf conversion.
It did not work inside a gitlab-ci pipeline at this time, nonetheless it work on my workstation (see here) with a third possibility : a hook !
(here with pdfkit)
require 'pdfkit'
module Jekyll
Jekyll::Hooks.register :site, :post_write do |post|
post.posts.docs.each do |post|
filename = post.site.dest + post.id + ".pdf"
dirname = File.dirname(filename)
Dir.mkdir(dirname) unless File.exists?(dirname)
kit = PDFKit.new(post.content, :page_size => 'Letter')
kit.stylesheets << './css/bootstrap.min.css'
kit.to_file(filename)
end
end
end
I am currently editing a file, and I'm using irb to test the api:
> require './file.rb'
> o = Object.new
> o.method
I then want to be able to edit the file.rb, and be able to see changes immediately. Example: assume new_method did not exist when I first required file.rb:
> o.new_method
Which will return an error. Is there a sandbox/developer mode or a method whereby I can achieve the above without having to reload the file every time? Require will not work after the first require, regardless. I assume worst case I'd have to use load instead.
I usually create a simple function like this:
def reload
load 'myscript.rb'
# Load any other necessary files here ...
end
With that, a simple reload will re-import all of the scripts that I'm working on. It's not automatic, but it's the closest thing that I've been able to come up with.
You may be able to override method_missing to call this function automatically when your object is invoked with a method that doesn't exist. I've never tried it myself, though, so I can't give any specific advice. It also wouldn't help if you're calling a method that already exists but has simply been modified.
In my own laziness, I've gone as far as mapping one of the programmable buttons on my mouse to the key sequence "reload<enter>". When I'm using irb, all it takes is the twitch of a pinky finger to reload everything. Consequently when I'm not using irb, I end up with the string "reload" inserted in documents unintentionally (but that's a different problem entirely).
This won't run every command, but you can include a file on every IRb session. ~/.irbrc is loaded each time you start an IRb session.
~/.irbrc
require "~/somefile.rb"
~/somefile.rb
puts "somefile loaded"
terminal
> irb
somefile loaded
irb(main):001:0>
~/.irbrc is loaded each time you start an irb session
What about require_dependency from ActiveSupport library?
require 'active_support/dependencies' #load it at the beginning
require_dependency 'myscript.rb'
Then require_dependency should track the changes in myscript file and reload it.
I'm new to Ruby
MakModule.rb
module Display
class Multiply
def Multiply.mul(first, second)
return first * second
end
end
end
MakRequire1.rb
require "Display"
puts Multiply.mul 5,6
MakRequire2.rb
require "MakModule.rb"
puts Multiply.mul 5,6
both file give me the error below
ruby: No such file or directory -- makRequire (LoadError)
How should I correct my code?
It is simply impossible that the code you posted here generates that error message. The error message says that you tried to require a file named makRequire, but that filename doesn't appear anywhere in the code you posted.
Without the actual code that is generating the actual error, it is impossible to answer your question. However, here are a few general tips:
Whenever the computer tells you that it cannot find something, in 99% of the cases, the problem is that the thing the computer tells it couldn't find isn't actually there.
So, in this case, the computer tells you that it cannot find a file named makRequire.rb, and the most likely explanation for that is that makRequire.rb doesn't actually exist. So, the first thing you need to check is: does makRequire.rb (note the capitalization and the file extension) actually exist? Because if it doesn't exist, then the reason why the computer cannot find it, should be rather obvious.
In 99% of the rest of the cases, the problem is that the thing the computer is looking for does exist, but the computer is looking in the wrong place. So, after you have verified that makRequire.rb actually does exist, you need to make sure that the directory the file is in, is in Ruby's $LOAD_PATH, and if it isn't, you need to add that directory to Ruby's $LOAD_PATH. Alternatively, if you want to require the file relative to the path of the file that is doing the requiring, you need to use require_relative instead of require.
The third thing to check for, is whether the user who own the ruby process has sufficient privileges to access the file makRequire.rb, the directory it is in and all of its parent directories.
Try this,
require File.join(File.dirname(__FILE__),'MarkModule')
Try require './MakModule', because the . is the current directory.
require 'MakModule'
You can require a file that is in the same directory. To use a module you would typically include the module inside a class definition. So you would never require Display, you would require the file that contains Display (without the .rb extension, usually).