calling a ruby method in the ruby terminal

ruby call method by name
how to call a method in ruby
ruby call method from another file
ruby call function from command line
ruby call function on self
ruby call module method
call static method ruby
ruby methods list

i have a method that gives an amount of square numbers depending on the number a user gives. eg if the users input is 5, the result will be 1, 4, 9, 16, 25. The method works. i was just wondering how to run this in the terminal. I know i need to do ruby squares.rb, which is the file name, but that just doesn't do anything afterwards. What i would like to happen is that someone could type squares(3), in the terminal, and get the result below it. im sure this is very simple aha, thanks.

def squares(input)
  numbers = (1..input)
  numbers.each do |number|
    puts number * number
  end
end

You can use gets method to get user input then parse it as int and call squares on it.

Try this

def squares(input)
  numbers = (1..input)
  numbers.each do |number|
    puts number * number
  end
end

def setup_input_loop
  loop do
    puts "Print square from 1 to n. Please enter n."
    input = gets.chomp.to_i
    squares(input)
    puts
  end
end

setup_input_loop

Ruby Programming/Syntax/Method Calls, may be defined as a part of a class or separately. Ruby methods are very similar to functions in any other programming language. Ruby methods are used to bundle one or more repeatable statements into a single unit. Method names should begin with a lowercase letter. If you begin a method name with an uppercase letter, Ruby might think that it is a constant and hence can parse the call incorrectly.


If you want a shell function that calls that ruby method:

squares() {
    ruby -e '
      def squares(input)
        numbers = (1..input)
        numbers.each do |number|
          puts number * number
        end
      end
      squares ARGV.shift.to_i
    ' -- "$1"
}

then

$ squares 3
1
4
9

If, by "ruby terminal", you mean irb then add that method to your ~/.irbrc file, then you can do

$ cat ~/.irbrc
def squares(input)
  numbers = (1..input)
  numbers.each do |number|
    puts number * number
  end
end

$ irb
irb(main):001:0> squares(3)
1
4
9
=> 1..3
irb(main):002:0> 

Just for fun, monkey patching the Integer class:

$ cat ~/.irbrc
class Integer
  def squares
    1.upto(self) {|n| puts n * n}
    self
  end
end
$ irb
irb(main):001:0> 3.squares
1
4
9
=> 3

Setup Ruby, Calling methods. In Ruby, methods that belong to (are defined on) objects can be used (called) by adding a dot, and then the method name, like so: object. In the example above, calling the method arbo without any arguments, is akin to sending a message with just “arbo” as the argument. Ruby supports the message sending idiom more directly, by including the send method in class Object (which is the parent of all objects in Ruby). So the following three lines are equivalent to the arbo method call:


Here's a variation that can be executed without prepending ruby to the filename and also doesn't make an infinite loop.

# make a file called square_loop.rb

#!/usr/bin/env ruby
def squares(input)
  numbers = (1..input)
  numbers.each do |number|
    puts number * number
  end
end

def setup_input_loop
  loop do
    puts "Print square from 1 to n. Please enter n or X to exit"
    input = gets.chomp
    exit if input.downcase == 'x' 
    squares(input.to_i)
    puts
  end
end

setup_input_loop

Then just make it executable with

chmod +x square_loop.rb

Then call it from your terminal with ./square_loop.rb

Though prepending ruby works too

ruby square_loop.rb

Is there a "main" method in Ruby like in C?, You can follow along using IRB by typing irb in your terminal and copying the Variables are a mechanism to teach your Ruby program about data; methods teach By defining the method say_hello_world_ten_times once, we can "call" or  #!/usr/bin/env ruby class A def run p :Hello_world end self end.new.run The usual way to script Ruby is to just use the top level execution environment called main. You can just start defining methods and code you write outside of a class, and these will be executed directly.


Learning Ruby methods allows the same piece of code to be executed many Many programming languages called this a procedure - in Ruby, we call it a Run this code using the ruby say.rb command from your terminal to see the result​. This means your Ruby program will end. How to Use the Popen Method For Two Way Communication With An External Program. If you need: More control over the process; Two-way communication; Then the IO.popen method is what you are looking for. In the following example I use the popen method to launch an irb process, then send some input to it


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  Calling Methods ¶ ↑ Calling a method sends a message to an object so it can perform some work. In ruby you send a message to an object like this: my_method () Note that the parenthesis are optional: my_method


tutorials, nor from the book I am reading, called “Ruby: the foundations You need a section at the end that actually calls the test method, you Now let's call these methods by passing integer values: add(20, 45) => 65 # returns 65 subtract(80, 10) => 70 # returns 70 What is less obvious is that Ruby actually allows us to pass a method call as an argument to other methods. Stated differently, we're saying we can pass add(20, 45) and subtract(80, 10) as arguments to another method.