Programming Ruby: The Pragmatic Programmer's Guide. Ruby - Remove double quotes from string. How To Write a Command Line Ruby Gem - Rob Dodson talks internets. So yesterday we saw how to setup and run ruby scripts as executables from the command line. While this is pretty rad, it definitely has its limitations. For one, it’s not very portable and secondly it just isn’t very flexible or powerful. If we stuck with this approach we’d need to write our own semantic versioning, we’d have to setup a way to make sure that all of our required 3rd party gems get installed properly… really it’ll just be a big mess if we try to hand-roll it.
Instead we’re going to turn to Bundler to help us manage our files and turn our command line tool into a ruby gem. I’m going to start fresh and create a totally new tool, called zerp. I don’t know what zerp’s purpose will be in the long run, but today we’re going to make it print some text to verify everything is working. New RVM Gemset Before I do anything with gems I want to make sure I have a cleanroom of sorts. Rvm gemset create zerp rvm gemset use zerp Now that we have our cleanroom we can template out a new gem. to. What does __FILE__ mean in Ruby. Rake Tutorial -- Handling Common Actions - { |one, step, back| } Rake is a tool for controlling builds. In this part of the Rake tutorial, we see how to organize the Rake actions to apply to many similar tasks. In the RakeTutorialIntroduction, we talked about the basics of specifying dependencies and associating actions to build the files.
We ended up with a nice Rakefile that built our simple C program, but with some duplication in the build rules. But First, Some Extra Rake Targets But before we get into all that, lets add some convience targets to our Rakefile. Task :default => ["hello"] Until now, the only kind of task we have seen in Rake are file tasks. A non-file task (or just plain “task”) does not represent the creation of a file. Here are a couple of other really useful tasks that I almost always include in a Rakefile. clean: Remove temporary files created during the build process. clobber: Remove all files generated during the build process. clobber is like clean, but even more aggressive. The answer is File lists. File Lists to the Rescue Up Next. An Intro to Web Scraping | The Bastards Book of Ruby. For journalists and researchers, being able to web-scrape is perhaps one of the most compelling reasons to learn programming.
Agencies and organizations don't always release their information in nicely-formatted databases. Learning to web scrape allows you to gather, in an automated fashion, freely available data in virtually any kind of online format. It's also one of the best ways to practice programming because the end goal is clear: either you have the data or you don't. And there's little harm to be done, provided your program scrapes at a reasonable rate. This chapter introduces the strategy of web scraping with a very non-technical overview of how websites work.
We won't cover any code. But knowing about give you the insight to immediately know how to traverse and parse any given website. Scraping: The big picture Rather than cover the concepts of GET, POST, HTTP requests, here's a broad overview of what happens when you interact with a website and submit a request. 1. 2. 3. 4. 5.
Ruby Screen-Scraper in 60 Seconds. By Ilya Grigorik on February 04, 2007 I often find myself trying to automate content extraction from a saved HTML file or a remote server. I've tried a number of approaches over the years, but the dynamic duo of Hpricot and Firebug blew me away - this is by far the fastest way to get what you want without compromising flexibility. Hpricot is an extremely powerful ruby-based HTML parser, and Firebug is arguably the best on-the-fly development add-on for Firefox. Now, I said it will take you about 60 seconds. I lied, it should take less. Let's get right to it. Introducing open-uri Ruby comes with a very flexible, production ready library that wraps all http/https connections into a single method call: open.
FireBug kung-foo Now that we have the document, we need to pull out some content that interests us - usually, this is the tedious part based on regular expressions, stream parsers, etc. So which came first, the parser, which will extract this, or this quote? Hpricot magic. Ruby Programming/Syntax/Method Calls. A method in Ruby is a set of expressions that returns a value. With methods, one can organize their code into subroutines that can be easily invoked from other areas of their program.
Other languages sometimes refer to this as a function. A method may be defined as a part of a class or separately. Method Calls[edit] Methods are called using the following syntax: method_name(parameter1, parameter2,…) If the method has no parameters the parentheses can usually be omitted as in the following: If you don't have code that needs to use method result immediately, Ruby allows to specify parameters omitting parentheses: results = method_name parameter1, parameter2 # calling method, not using parentheses # You need to use parentheses if you want to work with the result immediately.# e.g., if a method returns an array and we want to reverse element order: results = method_name(parameter1, parameter2).reverse Method Definitions[edit] Example: def output_something(value) puts value end Return Values[edit]
Installing Ruby 1.9.3 on Ubuntu 12.04 Precise Pengolin (without RVM) – Shizzle. Ruby 'require' error: cannot load such file. Unable to use gems in simple scripts. Setting up RVM, Ruby and Rails on Ubuntu 12.04 | Pragash. How do I transfer files using SSH and SCP using Ruby calls. Net::SSH: a pure-Ruby implementation of the SSH2 client protocol. Mitchellh/net-ssh-shell. How to SSH to a server using Ruby – Part I. Posted by sood on Thursday, January 20th, 2011 in Programming - Ruby/Rails Welcome to Part I of how to SSH to a Linux, Windows, etc. server using Ruby. This post will give you a complete code example to get you up and running right away! The code below will connect to the server specific and run the ls -al command.
Be sure to change the values in the example of @hostname, @username, @password, and @cmd to your own values. Ruby Syntax require 'rubygems' require 'net/ssh' @hostname = "localhost" @username = "root" @password = "password" @cmd = "ls -al" begin ssh = Net::SSH.start(@hostname, @username, :password => @password) res = ssh.exec!
Part I – How to SSH using Ruby, a simple example Part II – How to SSH using Ruby, with command-line arguments. How to SSH to a server using Ruby – Part II. Posted by sood on Thursday, January 20th, 2011 in Programming - Ruby/Rails Welcome to Part II of how to SSH to a server using Ruby. In Part I of this article, we gave you a simple example of how to SSH to a server and run a command.
The hostname, username, password, and the command to execute were hardcoded in the example. In this article, we will expand on the first example by allowing you to specify the server connection information and the command to execute as command-line parameters. This will allow you to use the same script to connect to a variety of servers and execute any command without modifying the script over and over. Ruby Syntax Example of how to execute the script ruby script_name.rb -h localhost -u root -p password -c "ls -al" As you can see, the hostname, username, password, and command are all parameters passed in to the script. Part I – How to SSH using Ruby, a simple example Part II – How to SSH using Ruby, with command-line arguments 25% Off All Hosting Plans: HW25PERCENT. Net::SSH Manual :: Chapter 2: Starting a Session. Before you can do anything with Net::SSH, you need to require the net/ssh module: Requiring Net::SSH [ruby] Once you have required the net/ssh module, you can begin an SSH session by calling Net::SSH.start.
This may be used in one of two ways. If called without a block, it will return a reference to the new session as an instance of a Net::SSH::Session. Used this way, you must explicitly close the session when you are finished with it. Opening an SSH session [ruby] The other approach involves attaching a block to the start method. Opening a transactional SSH session [ruby] If you need to specify a different port on the host to connect to (the default is 22), you can specify it immediately after the host parameter, like so: Specifying the SSH port [ruby] Using Keyword Arguments Some people prefer using keyword arguments for functions with more than a couple of parameters.
Using keyword arguments [ruby] (More about the “...” stuff, later.) Failed Authentication Setting up public/private keys. Net::SSH Manual :: Chapter 3: Channels. 3.1. What are Channels? The SSH protocol requires that requests for services on a remote machine be made over channels. A single SSH connection may contain multiple channels, all run simultaneously over that connection. Each channel, in turn, represents the processing of a single service. When you invoke a process on the remote host with Net::SSH, a channel is opened for that invocation, and all input and output relevant to that process is sent through that channel.
The connection itself simply manages the packets of all of the channels that it has open. This means that, for instance, over a single SSH connection you could execute a process, download a file via SFTP, and forward any number of ports, all (seemingly) at the same time! Naturally, they do not occur simultaneously, but rather work in a “time-share” fashion by sharing the bandwidth of the connection. 3.2. The loop method is easy to invoke: Session#loop [ruby] 3.3. Each channel has a type. 3.4. Opening a channel [ruby] 3.5. 3.6. 2.x Getting Started · capistrano/capistrano Wiki. This tutorial will walk you through the basics of setting up and using Capistrano. It will not introduce you to the deployment system that is bundled with Capistrano, but will instead focus on the more general areas of executing Capistrano and writing your own recipes.
It will be primarily of interest to those wanting to use Capistrano in non-deployment domains, and to those who just wish to become more familiar with Capistrano itself. Installation Capistrano is actually comprised of the Capistrano gem. $ gem -v Your RubyGems should be at least 1.3.x, if not please follow their upgrade instructions and come back here... with RubyGems up to date, you can install Capistrano and its dependencies with the following: $ gem install capistrano Assumptions Capistrano makes a few assumptions about your servers. You are using SSH to access your remote machines. To take full advantage of Capistrano, you should be comfortable (or at least, minimally familiar) with the Ruby programming language.
Capfile. Capistrano and Net:SSH with login shell. By Johan Lundahl - Dynamic languages, Tips & Tricks New to Capistrano? Read my previous and more introductory blog post on Capistrano – Remote builds with Capistrano. Are you using Capistrano and are confused why the user environment, the PATH for example, is different from when you log on to your server via SSH? Perhaps you have failed to use RVM or other user specific applications when running your Capistrano scripts? This post will explain why this is, and what can be done to solve the problem. By default Capistrano will execute remote commands by something like so: Net::SSH.start( host, user, password: pass ) do |ssh| ssh.open_channel do |channel| channel.exec("sh -c '#{command}'") end end There is no interactive shell and only the system’s environment is loaded. With Net:SSH there are at least two ways to execute commands in a login shell.
The second way is to make your shell act as if it had been invoked as a login shell. Now we know that Capistrano does not give us a user shell. Use ruby Net::SSH to read a remote file via sudo. Help with net-ssh shell-script timing. Respond to a SSH prompt before first command with ruby and Net::SSH.