Quantcast
Channel: How do I define a method on one line in Ruby? - Stack Overflow
Browsing all 7 articles
Browse latest View live

Answer by Dan for How do I define a method on one line in Ruby?

Ruby 3.0.0 adds "endless" definitions for methods with exactly one statement:def greet = puts("hello")Note that the one-statement limitation means that this can't be written as:# NOT ALLOWEDdef greet =...

View Article



Answer by Sergey Alekseev for How do I define a method on one line in Ruby?

No Single-line MethodsFrom rubocop/ruby-style-guide#no-single-line-methods:Avoid single-line methods. Although they are somewhat popular in thewild, there are a few peculiarities about their definition...

View Article

Answer by Lifeweaver for How do I define a method on one line in Ruby?

Yet another way:def greet() return 'Hello' end

View Article

Answer by horseyguy for How do I define a method on one line in Ruby?

You can avoid the need to use semicolons if you use parentheses:def hello() :hello end

View Article

Answer by Victor Deryagin for How do I define a method on one line in Ruby?

Another way:define_method(:greet) { puts 'hello' }May be used if you don't want to enter new scope for method while defining it.

View Article


Answer by edgerunner for How do I define a method on one line in Ruby?

def add a,b; a+b endThe semicolon is the inline statement terminator for RubyOr you can use the define_method method. (Edit: This one's deprecated in ruby 1.9)define_method(:add) {|a,b| a+b }

View Article

How do I define a method on one line in Ruby?

Is def greet; puts "hello"; end the only way to define a method on one line in Ruby?

View Article
Browsing all 7 articles
Browse latest View live




Latest Images