background preloader

Ruby in Twenty Minutes

Ruby in Twenty Minutes
Introduction This is a small Ruby tutorial that should take no more than 20 minutes to complete. It makes the assumption that you already have Ruby installed. Interactive Ruby Ruby comes with a program that will show the results of any Ruby statements you feed it. Open up IRB (which stands for Interactive Ruby). If you’re using macOS open up Terminal and type irb, then hit enter. irb(main):001:0> Ok, so it’s open. Type this: "Hello World" irb(main):001:0> "Hello World" => "Hello World" Ruby Obeyed You! What just happened? irb(main):002:0> puts "Hello World" Hello World => nil puts is the basic command to print something out in Ruby. Your Free Calculator is Here Already, we have enough to use IRB as a basic calculator: irb(main):003:0> 3+2 => 5 Three plus two. irb(main):004:0> 3*2 => 6 Next, let’s try three squared: irb(main):005:0> 3**2 => 9 In Ruby ** is the way you say “to the power of”. irb(main):006:0> Math.sqrt(9) => 3.0 Ok, wait, what was that last one? Modules Group Code by Topic

Parsing an text file Hi, I cannot find a way to easily parse a text file. I am just starting to learn ruby so please bear with me. My text file looks like this: 0 8 2 9 3 0 0 4 9 2 8 3 9 3 0 2 2 3 4 9 8 8 9 0 Basically just numbers and spaces. So far, I know how to read the file and put each line into an array. def readfile(file) arr = IO.readlines(file) parsing(arr) end def parsing(arr) string1 = arr[0].to_s string2 = arr[1].to_s string3 = arr[2].to_s ans.each_byte {|b|p b.chr} end What I wanted to do is, for example, want to find all 8's and change it to 5's. I am stuck on how I can use the string and search each char's to find the 8's and then change it. Also I would like to know if there is a for loop or something to go through each array and store it into a new string because there might be more than 3 lines. Thanks for your fast answer. Johnathan Wagner wrote: > Thanks for your fast answer.>> One last question, if you can help me.>> How can I compare each characters in a string?

Just Enough Ruby for Chef - Chef - Opscode Open Source Wiki Ruby is a simple programming language that is designed to be easy to read and to behave in a predictable manner. The chef-client uses Ruby as its reference language for creating cookbooks and defining recipes, with an extended DSL (domain-specific language) that is used for specific resources. Enough resources are available to support the most common infrastructure automation scenarios natively within Chef; however, this DSL can also be extended when additional resources and capabilities are required. The chef-client uses Ruby as its reference language for creating cookbooks and defining recipes, with an extended DSL (domain-specific language) that is used for specific resources. Many people who are new to Ruby often find that it doesn’t take very long to get up to speed with the basics. $ ruby -c my_cookbook_file.rb to return: Here is a quick rundown of some basic Ruby commands. Anything after a # is a comment. Assign a local variable: Do some basic arithmetic: Work with strings: ! !!

Top 5 Tutorials on Ruby on Rails Ruby on Rails is a new kid in the block into the world of Web application development that is rapidly gaining interest, even though it is in beta versions. These days, I am trying to get my hands dirty with Ruby on Rails using various tutorial posts and also some books. Over the coming days, i will try to share my learnings with you through the blog. Below, in this post, I am putting up a list of Top 5 Tutorials to get you started on Ruby on Rails (RoR). These are infact tutorials, i am going through in order to learn RoR. Ruby on Rails Tutorial for Newbies: This tutorial is a basic tutorial targeting the newbies in RoR arena.OnLamp’s Tutorial-Rolling with Ruby on Rails: Author Curt Hibbs has given an excellent post on RoR in his tutorial. While the above listed tutorials are really great, i also recommend that you go through Ruby on Rails guides.

Getting Started with Nokogiri | Engine Yard Ruby on Rails Blog We're decided to mix up the Engine Yard blog a little and invite some community members to contribute guest posts. This one (our first!) is from [Aaron Patterson]( -- a long-time member of the Ruby community, and the creator of Nokogiri. He hacks with the developers of Seattle.rb, and travels the world to speak about Nokogiri and other Ruby topics at industry conferences and events. Nokogiri is a library for dealing with XML and HTML documents. Getting Nokogiri installedBasic document parsingBasic data extraction Hopefully by the end of this article you will also be able to use and enjoy Nokogiri on a day to day basis too! Installation Nokogiri is actually a wrapper around Daniel Veillard's excellent HTML/XML parsing library written, libxml2. I recommend installing libxml2 on OS X from macports. To install libxml2 from macports: $ sudo port install libxml2 libxslt Then to install nokogiri: $ sudo gem install nokogiri And that should be it! Linux On Fedora: On Ubuntu:

Ruby QuickRef Table of Contents Language General Tips These are tips I’ve given over and over and over and over… Use 2 space indent, no tabs. Use [] over Array.new. See for more. General Syntax Rules Comments start with a pound/sharp (#) character and go to EOL. Reserved Words alias and BEGIN begin break case class def defined? Types Basic types are numbers, strings, ranges, regexen, symbols, arrays, and hashes. Numbers 1231_234123.451.2e-30xffff 0b01011 0377 ? Strings In all of the %() cases below, you may use any matching characters or any single character for delimiters. %[], %!! 'no interpolation'"#{interpolation}, and backslashes\n"%q(no interpolation)%Q(interpolation and backslashes)%(interpolation and backslashes)`echo command interpretation with interpolation and backslashes`%x(echo command interpretation with interpolation and backslashes) Backslashes: Here Docs: Encodings: Waaaay too much to cover here. Symbols Internalized String. Ranges Files

Programmation Ruby/Types standards Un livre de Wikilivres. << Retour au sommaire Nous allons voir ici tous les types que nous pouvons considérer comme "standards", dans le sens où nous les retrouvons dans la plupart des langages, et que ceux-ci sont directement intégrés à l'interpréteur (built-in). Néanmoins il ne faut pas perdre de vue qu'il s'agit en réalité d'objets. Pour rappel les méthodes dont le nom se termine par ! Chaîne de caractères[modifier | modifier le wikicode] En ruby les chaînes de caractères sont représentées par l'objet "String". Créer une chaîne de caractères[modifier | modifier le wikicode] En ruby il existe une multitude de manière de créer une chaîne de caractères, par exemple en créant une instance de l'objet String : maChaine = String.new("Une chaîne de caractères") Mais le moyen le plus courant de créer une chaîne est de la placer entre simple quote (') ou entre double quote ("). Dans une chaîne de caractère \ à une valeur particulière, elle permet de placer un caractères d'échappements. %q! "Ho!

How to parse CSV data with Ruby - Ruby - csv, parse, ruby, fastercsv, ccsv, csvscan, excelsior Ruby alternatives for parsing CSV files Ruby String#split (slow) Built-in CSV (ok, recommended) ccsv (fast & recommended if you have control over CSV format) CSVScan (fast & recommended if you have control over CSV format) Excelsior (fast & recommended if you have control over CSV format) CSV library benchmarks can be found here and here Parsing with plain Ruby filename = 'data.csv' file = File.new(filename, 'r') file.each_line("\n") do |row| columns = row.split(",") break if file.lineno > 10end This option has several problems... Parsing with the built-in CSV library require 'csv' CSV.open('data.csv', 'r', ';') do |row| puts row end require 'csv' CSV.foreach("changes.csv", quote_char: '"', col_sep: ';', row_sep: :auto, headers: true) do |row| puts row[0] puts row['xxx']end Parsing with the ccsv library ccsv is hosted on GitHub. require 'rubygems'require 'ccsv' Ccsv.foreach(file) do |values| puts values[0]end Parsing with the CSVScan library CSVScan can be downloaded from here.

Ruboto: Ruby on Android Introduction Most of the interesting work in software development today occurs at one of the two extremes: huge cloud servers and tiny mobile devices. These domains solve significantly different problems, and, correspondingly, have different tool support. Server development often uses scripting languages to tie together different components and accomplish sophisticated automated tasks, while mobile development focuses on the particular capabilities and needs of a particular device and user. However, these two extremes share a common language: Java™. This article explores Ruboto, a project that bridges the gap between scripting languages and Android. Back to top Ruby background Many scripting languages compete for programmers' affections, but Ruby currently holds a powerful position. Ruby comes in several flavors, including the popular JRuby. Android background Android was developed by the Open Handset Alliance, but it is most often thought of as a Google project. Android + Ruby = Ruboto Java

ruby - inserting and deleting nokogiri XML nodes and elements

Related: