background preloader

Fctions

Facebook Twitter

Advanced Ruby Arrays. From Techotopia Purchase and download the PDF and ePub versions of this Ruby eBook for only $8.99 In the Understanding Ruby Arrays we looked at the basics of arrays in Ruby.

Advanced Ruby Arrays

In this chapter we will look at arrays in more detail. [edit] Combining Ruby Arrays Arrays in Ruby can be concatenated using a number of different approaches. Days1 = ["Mon", "Tue", "Wed"] days2 = ["Thu", "Fri", "Sat", "Sun"] days = days1 + days2 => ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] Alternatively, the concat method may be called: days1 = ["Mon", "Tue", "Wed"] days2 = ["Thu", "Fri", "Sat", "Sun"] days = days1.concat(days2) Elements may also be appended to an existing array using the << method.

Days1 = ["Mon", "Tue", "Wed"] days1 << "Thu" << "Fri" << "Sat" << "Sun" => ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] [edit] Intersection, Union and Difference Ruby's support for array manipulation goes beyond that offered by many other scripting languages. A few examples will help to clarify these operations. Add new text to files. Chris Conley (Guest) on 2008-06-19 00:54 Hello, I'm trying to insert new text into the middle of a file with the following: file = File.open("file.rb", "r+") file.each { |line| if line.match(/line text/) file.puts("hello") end } This works fine except that it overwrites the existing 5 characters to replace with "hello".

Add new text to files

Is there a way to insert a new line with new text without overwriting any existing data? Thanks! Chris Chris Conley wrote: > Hello,>> I'm trying to insert new text into the middle of a file with the> following:>> file = File.open("file.rb", "r+")> file.each { |line|> if line.match(/line text/)> file.puts("hello")> end> }>> This works fine except that it overwrites the existing 5 characters to> replace with "hello". Rob Biedenharn (Guest) on 2008-06-19 01:39. Working with Files in Ruby. From Techotopia Purchase and download the PDF and ePub versions of this Ruby eBook for only $8.99 In the previous chapter we looked at how to work with directories.

Working with Files in Ruby

This chapter will look in detail at how to create, open and read and write to files in Ruby. We will then learn how to delete and rename files. [edit] Creating a New File with Ruby New files are created in Ruby using the new method of the File class. With this information in mind we can, therefore, create a new file in "write" mode as follows: File.new("temp.txt", "w") => #<File:temp.txt> [edit] Opening Existing Files Existing files may be opened using the open method of the File class: Rubular: a Ruby regular expression editor and tester.

File Access. Tableaux. Class: ActiveRecord. Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

Class: ActiveRecord

Adding, removing, and changing attributes and their type is done directly in the database. Any change is instantly reflected in the Active Record objects. The mapping that binds a given Active Record class to a certain database table will happen automatically in most common cases, but can be overwritten for the uncommon ones. See the mapping rules in table_name and the full example in files/activerecord/README_rdoc.html for more insight. Creation Active Records accept constructor parameters either in a hash or as a block. User = User.new(name: "David", occupation: "Code Artist") user.name You can also use block initialization:

Factory