An examination of "Put" in Ruby

An examination of “Put” in Ruby :

this one is like your scripts with ARGV

def print_two(*args)
arg1, arg2 = args
puts “arg1: #{arg1}, arg2: #{arg2}”
end

ok, that *args is actually pointless, we can just do this

def print_two_again(arg1, arg2)
puts “arg1: #{arg1}, arg2: #{arg2}”
end

this just takes one argument

def print_one(arg1)
puts “arg1: #{arg1}”
end

this one takes no arguments

def print_none()
puts “I got nothin’.”
end

print_two(“Zed”,“Shaw”)
print_two_again(“Zed”,“Shaw”)
print_one(“First!”)
print_none()

First off, puts is not a function. It’s sole purpose is to have a side-effect (printing something to the console), whereas functions cannot have side-effects … that’s the definition of “function”, after all.

Ruby doesn’t have functions. It only has methods. Thus, puts is a method.

Is this true ? So basically, ‘puts’ is just a “thing” to enable printing something to the console …

Yes. Puts is just a method. The side effect of puts is to write to the console. That’s not the entire story, but that’s a good start right now.

*args will become more useful to you as you learn the language more.

def print_many(*args)
  args.each do |argument|
    puts argument
  end
end

print_many "1", "2", "3"
# 1
# 2
# 3
1 Like