Ruby want to read file line by line and get the particular part out using gsub
Strings Don't Normally #respond_to? :each
The stack trace tells you everything you need to know:
undefined method `each' for #String:0x00007fef3d0907c0 (NoMethodError)
Even assuming that /channels /
with a trailing space is a valid portion of the file path, File#open returns a File object rather than a collection of lines. As written, video_links is a String, not a collection such as a Hash or Array, and there's no String#each method. Since for-in loops are syntatic sugar for #each, the object can't respond to the method.
Depending on whether you want to slurp the whole file into an array of lines, or operate linewise, you should use one of the following alternative methods:
File#each_line, inhereted from IO. For example:
File.open("path/to/file").each_line
File#readlines, also inherited from IO. For example:
File.readlines("path/to/file")
How to execute multiple succeeding functions in 1 line in Ruby?
One easy way is to just parenthesize the statements:
ruby-1.9.1-p378 > 0.upto(5) do |n|
ruby-1.9.1-p378 > (puts n; break;) if n == 3
ruby-1.9.1-p378 ?> puts ">>#{n}<<"
ruby-1.9.1-p378 ?> end
>>0<<
>>1<<
>>2<<
3
If it's a bit much to put in parentheses, a begin-end will do the trick:
0.upto(5) do |n|
begin
puts "I found a matching n!"
puts n
puts "And if you multiply it by 10, it is #{10*n}"
break;
end if n == 3
puts "((#{n}))"
end
Output:
((0))
((1))
((2))
I found a matching n!
3
And if you multiply it by 10, it is 30
Ruby script start from a specific line or method
There are ways as the answer of max shows you, but you shouldn't do it.
There were questions about a goto statement for Ruby before you know.
Why should you go back to the techniques used in BASIC ? Show us a usage and we'll show you better ways.
If you are begining with Ruby and programming you can just use a procedural way of programming like
def print_a
puts "a"
end
and later on, no matter where the line of "puts a" has moved to in between you last update
print_a
If you are more experienced you'll use this with a combination of Object Oriented or Functional way of programming.
One acceptional use comes me in to mind: conditional require 's or load's
if condition
require ./mycode_only_to be_executed_in_this_case.rb'
end
Hope I got you rethinking your question..
EDIT after comment op
The example you refer to isn't DRY, that is the first thing that I' m noticing.
See https://en.wikipedia.org/wiki/Don't_repeat_yourself for a definition of DRY
Here what I would make of it, I still use a methods to envelop my code so that it is code that I can reuse and has only one purpose and is testable: connect to a site. No duplicate code, so DRY.
require 'watir'
def goto_site site
a = Watir::Browser.new :chrome
a.goto site
# a lot of code down here
a.close
end
goto_site 'site.com'
run ruby from within an IDE and have each piece of code return a value
Use the following process for Komodo Edit:
- Click Tools=>Run Command
- In the Run field, enter
%(ruby) -i %f
- In the Run in: field, select
New Console
orCommand Output Tab
- Check
Add to Toolbox
References
- ActiveState Komodo: Run Commands
output a line to a line before in ruby
f.puts "The result is: #{my_array.flatten.join(" ")}"
Related Topics
How Do Version Numbers Work for Mri Ruby
How to Count Existing Instances of a Class in Ruby
Splitting String into Pair of Characters in Ruby
How to Parse a Url and Extract the Required Substring
Stop Loading Page Watir-Webdriver
Ruby.Metaprogramming. Class_Eval
Ruby on Rails 4 Select Multiple
Authlogic and Multiple Sessions for the Same User
How to Memoize a Method That May Return True, False, or Nil in Ruby
Error Installing SQLite3 Gem via Bundler
Ruby on Linux Pty Goes Away Without Eof, Raises Errno::Eio
Add Existing Classes into a Module
What Does ["String"].Pack('H*') Mean
Installing Rmagick Gem -- Can't Find Magickwand.H
Differencebetween Ruby's 'Open-Uri' and 'Net:Http' Gems
How to List the Available Variables in an Ruby Erb Template
Opening Several Threads with Watir-Webdriver Results in 'Connection Refused' Error