Extract part of string in Ruby

So I'm given a text file containing:

Blade Runner (1982) [117 min] 
Full Metal Jacket (1987) [116 min] 
Monty Python and the Holy Grail (1975) [91 min] 
The Godfather (1972) [175 min]

and have to turn it into this:

Movie name: Blade Runner  
Movie release year: 1982 
Movie length (in mins): 117 

Movie name: Full Metal Jacket  
Movie release year: 1987 
Movie length (in mins): 116 

Movie name: Monty Python and the Holy Grail  
Movie release year: 1975 
Movie length (in mins): 91 

Movie name: The Godfather  
Movie release year: 1972 
Movie length (in mins): 175

First I iterate on each line, then I thought I should iterate on each part of the string, but that's where I get stuck, how do I do that? Do I use regex? How do I save a specific string that matches a regex?

Here's the current shell of the code, it stores the three parts into variables which are used to initialize a movie class who's to_s method prints in the desired format.

I know it's not right in many ways, but that's why I'm asking for help. variable = /regex/ is the line where the variable is assigned the thing captured by the regex and when /regex/ is for when that regex is matched.

class Movie
    def initialize (name, year, length) # constructor
        @name = name
        @year = year
        @length = length

    def to_s    # returns string representation of object
        return "Movie Name: " + @name
            + "\nMovie release year: "
            + @year + "\nMovie Length (in min): "
            + @length + "\n"

$movies = []
File.open("movies.txt").each do |line|
  if matches = /(.*)? \((\d+).*?(\d+)/.match(line)
    $movies << Movie.new(matches[1], matches[2], matches[3])

for $movie in $movies do #what u got here is not index but the element in the array
    print $movie.to_s


Fixed version of code, but print loop at end doesn't work.

Edit2: and nownit does. Thanks PeterPeiGuo!


m = /(.*)? \((\d+).*?(\d+)/.match("Blade Runner (1982) [117 min]")

Need Your Help

Update sql query with JOIN

php mysql sql

I have two table in my database.

Instantiating Custom Adapter by passing Activity

java android

I am trying to call the LazyAdapter constructor for setting the ListView. And I always see a red line on new LazyAdapter(this, mStrings); asking to resolve this. I am not sure what should I add her...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.