Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
15 Questions to Ask During a Ruby Interview

Originally published in June 2008

When hiring Ruby on Rails programmers, knowing the right questions to ask during an interview was a real challenge for me at first. In 30 minutes or less, it's difficult to get a solid read on a candidate's skill set without looking at code they've previously written. And in the corporate/enterprise world, I often don't have access to their previous work.

To ensure we hired competent ruby developers at my last job, I created a list of 15 ruby questions -- a ruby measuring stick if you will -- to select the cream of the crop that walked through our doors.

What to expect

Candidates will typically give you a range of responses based on their experience and personality. So it's up to you to decide the correctness of their answer.

There are many solutions to most of these questions -- some aren't listed here. Candidates get my respect for knowing esoteric solutions, but I'm looking for developers with similar practices to my own.

Make no mistake, this list is not complete, by any means. But it does provide a useful tool to put a candidate's reaction to the same series of questions in perspective. Especially during an interview, when your mouth goes dry and and your hands get all sweaty.

Begin!

Senior programmers won't have a problem with these, while junior programmers will usually give only half-answers.

What is a class?

A text-book answer: classes are a blue-print for constructing computer models for real or virtual objects... boring.

In reality: classes hold data, have methods that interact with that data, and are used to instantiate objects.

Like this.

class WhatAreClasses
  def initialize
    @data = "I'm instance data of this object. Hello."
  end

  def method
    puts @data.gsub("instance", "altered")
  end
end

object = WhatAreClasses.new
object.method
 #=> I'm altered data of this object. Hello.

What is an object?

An instance of a class.

To some, it's also the root class in ruby (Object).

Classes themselves descend from the Object root class. (Kudos to Ezra)

What is a module? Can you tell me the difference between classes and modules?

Modules serve as a mechanism for namespaces.

module ANamespace
  class AClass
    def initialize
      puts "Another object, coming right up!"
    end
  end
end

ANamespace::AClass.new
 #=> Another object, coming right up!

Also, modules provide as a mechanism for multiple inheritance via mix-ins and cannot be instantiated like classes can.

module AMixIn
  def who_am_i?
    puts "An existentialist, that's who."
  end
end

# String is already the parent class
class DeepString < String
  # extend adds instance methods from AMixIn as class methods
  extend AMixIn
end

DeepString.who_am_i?
 #=> An existentialist, that's who.

AMixIn.new
 #=> NoMethodError: undefined method ‘new’ for AMixIn:Module

Can you tell me the three levels of method access control for classes and modules? What do they imply about the method?

All methods, no matter the access control, can be accessed within the class. But what about outside callers?

Public methods enforce no access control -- they can be called in any scope.

Protected methods are only accessible to other objects of the same class.

Private methods are only accessible within the context of the current object.

class AccessLevel
  def something_interesting
    another = AccessLevel.new
    another.public_method
    another.protected_method
    another.private_method
  end

  def public_method
    puts "Public method. Nice to meet you."
  end

  protected

  def protected_method
    puts "Protected method. Sweet!"
  end

  private 

  def private_method
    puts "Incoming exception!"
  end
end

AccessLevel.new.something_interesting
 #=> Public method.  Nice to meet you.
 #=> Protected method.  Sweet!
 #=> NoMethodError: private method ‘private_method’ called for
 #=>  #<AccessLevel:0x898c8>

There are three ways to invoke a method in ruby. Can you give me at least two?

Here, I'm looking for the dot operator (or period operator), the Object#send method, or method(:foo).call

object = Object.new
puts object.object_id
 #=> 282660

puts object.send(:object_id)
 #=> 282660

puts object.method(:object_id).call # (Kudos to Ezra)
 #=> 282660

Separating the professional from the hobbyist

Senior programmers should be able to give competent answers for all questions. Junior programmers should answer some correct, but usually won't know them all.

Explain this ruby idiom: a ||= b

A common idiom that strong ruby developers use all the time.

# a = b when a == false
# otherwise a remains unchanged
a || a = b # (Kudos to Markus Prinz)
a = 1
b = 2
a ||= b #=> a = 1
a = nil
b = 2
a ||= b #=> a = 2
a = false
b = 2
a ||= b #=> a = 2

What does self mean?

self always refers to the current object. But this question is more difficult than it seems because Classes are also objects in ruby. (Kudos to Stephen)

class WhatIsSelf
  def test
    puts "At the instance level, self is #{self}"
  end

  def self.test
    puts "At the class level, self is #{self}"
  end
end

WhatIsSelf.test 
 #=> At the class level, self is WhatIsSelf

WhatIsSelf.new.test 
 #=> At the instance level, self is #<WhatIsSelf:0x28190>

This short snippet indicates two things:

  • at the class level, self is the class, in this case WhatIsSelf.
  • at the instance level, self is the instance in context, in this case the instance of WhatIsSelf at memory location 0x28190.

What is a Proc?

Everyone usually confuses procs with blocks, but the strongest rubyist can grok the true meaning of the question.

Essentially, Procs are anonymous methods (or nameless functions) containing code. They can be placed inside a variable and passed around like any other object or scalar value. They are created by Proc.new, lambda, and blocks (invoked by the yield keyword).

Note: Procs and lambdas do have subtle, but important, differences in ruby v1.8.6. However, I wouldn't expect a candidate talk about these nitty-gritty details during an interview. (Kudos to Noah Thorp)

# wants a proc, a lambda, AND a block
def three_ways(proc, lambda, &block)
  proc.call
  lambda.call
  yield # like block.call
  puts "#{proc.inspect} #{lambda.inspect} #{block.inspect}"
end

anonymous = Proc.new { puts "I'm a Proc for sure." }
nameless  = lambda { puts "But what about me?" }

three_ways(anonymous, nameless) do
  puts "I'm a block, but could it be???"
end
 #=> I'm a Proc for sure.
 #=> But what about me?
 #=> I'm a block, but could it be???
 #=> #<Proc:0x00089d64> #<Proc:0x00089c74> #<Proc:0x00089b34>

What is unit testing (in classical terms)? What is the primary technique when writing a test?

The strongest candidates should be quite comfortable with test or behavior driven development.

Unit testing, simply put, is testing methods -- the smallest unit in object-oriented programming. Strong candidates will argue that it allows a developer to flesh out their API before it's consumed by other systems in the application.

The primary way to achieve this is to assert that the actual result of the method matches an expected result.

require "test/unit"

class Brokened
  def uh_oh
    "I needs fixing"
  end
end

class BrokenedTest < Test::Unit::TestCase
  def test_uh_oh
    actual = Brokened.new
    assert_equal("I'm all better!", actual.uh_oh)
  end
end
 #=> Started
 #=> F
 #=> Finished in 0.663831 seconds.
 #=> 
 #=>   1) Failure:
 #=> test_uh_oh:11
 #=> <"I'm all better!"> expected but was
 #=> <"I needs fixing">.
 #=> 
 #=> 1 tests, 1 assertions, 1 failures, 0 errors

Show me the money!

Variable typing is one of those topics that everyone sort of understands it, but is hard to put it into words. I've iterated and improved the next series of questions to really test a senior level candidate's knowledge of static and dynamic typing. This is my best attempt so far.

What is the primary difference in these two code snippets?

// Java
public boolean isEmpty(String s) {
  return s.length() == 0;
}
# ruby
def empty?(s)
  return s.size == 0
end

The Java method only accepts Strings as arguments and only returns a boolean while...

The ruby method accepts any Object and could return anything, but in this case will return a boolean if executed without exceptions.

What does this say about the advantages of ruby's dynamic (duck) typed system?

That ruby program use less code and are more flexible.

What are some disadvantages (real and potential)?

Developers cannot be 100% certain that all arguments sent this empty? method will have a size method that is publicly accessible. Also, ruby is an interpreted language and it may take longer to run than compiled programs, such as Java, that are programmed similarly.

What could a developer do to address these disadvantages?

She could write unit tests or specs to ensure her application behaves as intended. She could also profile her application with tools like the unix time command, the ruby Benchmark class, and the ruby library called ruby-prof.

A cunning programmer would also argue that these two techniques ought to be used for both static and dynamic languages when developing complex systems.

Wrapping things up

To finish up with, I like to lob in some easy ones again. Plus I like to scratch my own curiosity about a candidates relationship with the ruby community.

What are rubygems? Any favorites not including rails? Any that you've worked on personally?

rubygems is package manager software for ruby libraries (i.e. gems). The package manager has basic CRUD operations, dependency trees, and supports asynchronous communication between multiple gem servers.

What is your favorite api resource for ruby?

I really like gotAPI -- auto-complete searching for both ruby and rails together!

Your Own Questions?

I really want to know how you interview candidates for your ruby positions! Ruby is being used heavily at many companies all over the world for external and internal use alike. Please share your ideas with the rest of us!

@punund

This comment has been minimized.

Show comment Hide comment
@punund

punund Apr 22, 2013

If you are interviewing for a Ruby position, why would you assume a knowledge of Java?

punund commented Apr 22, 2013

If you are interviewing for a Ruby position, why would you assume a knowledge of Java?

@Insood

This comment has been minimized.

Show comment Hide comment
@Insood

Insood Apr 26, 2013

I don't know any java, but I have some C++ knowledge and could divine the intent of the question.

Insood commented Apr 26, 2013

I don't know any java, but I have some C++ knowledge and could divine the intent of the question.

@cswagerty

This comment has been minimized.

Show comment Hide comment
@cswagerty

cswagerty Oct 31, 2013

This was an awesome preparation tool! Thanks for sharing!

I've also been asked if Ruby has open classes. And what does that mean. Probably since that's huge difference between Ruby and stricter languages.

This was an awesome preparation tool! Thanks for sharing!

I've also been asked if Ruby has open classes. And what does that mean. Probably since that's huge difference between Ruby and stricter languages.

@jhamon

This comment has been minimized.

Show comment Hide comment
@jhamon

jhamon Dec 2, 2013

@cswagerty Ruby does have open classes. This refers to our ability in Ruby to open up classes and add methods at any time after they have been defined. For example, to write your own Array extension, we open the Array class and add our method. Another way to do this with metaprogramming is to use define_method. Rails uses this special ability all the time.

class Array
  def my_silly_method
    # .....
  end
end

jhamon commented Dec 2, 2013

@cswagerty Ruby does have open classes. This refers to our ability in Ruby to open up classes and add methods at any time after they have been defined. For example, to write your own Array extension, we open the Array class and add our method. Another way to do this with metaprogramming is to use define_method. Rails uses this special ability all the time.

class Array
  def my_silly_method
    # .....
  end
end
@sladebot

This comment has been minimized.

Show comment Hide comment
@sladebot

sladebot Jan 19, 2014

This is an awesome gist for an overview on the expectations in a ruby intervew. I had also been asked 'What are anonymous methods, open classes etc. Thanks a lot for sharing this.

This is an awesome gist for an overview on the expectations in a ruby intervew. I had also been asked 'What are anonymous methods, open classes etc. Thanks a lot for sharing this.

@rpatil

This comment has been minimized.

Show comment Hide comment
@rpatil

rpatil Mar 10, 2014

Good One +1

rpatil commented Mar 10, 2014

Good One +1

@slindsey3000

This comment has been minimized.

Show comment Hide comment
@slindsey3000

slindsey3000 May 7, 2014

I read the "Pickaxe Book", "The Well Grounded Rubyist", and "Eloquent Ruby" and created this list of over 500 questions as I went along. It is sure to test your Ruby knowledge from the simple to the more complicated.

http://shawnlindsey.com/blog/ruby-test-questions-the-great-big-ruby-test

I read the "Pickaxe Book", "The Well Grounded Rubyist", and "Eloquent Ruby" and created this list of over 500 questions as I went along. It is sure to test your Ruby knowledge from the simple to the more complicated.

http://shawnlindsey.com/blog/ruby-test-questions-the-great-big-ruby-test

@Sessl

This comment has been minimized.

Show comment Hide comment
@Sessl

Sessl May 28, 2014

This is very helpful. Thanks Ryan, slindsey and others for your contributions.

Sessl commented May 28, 2014

This is very helpful. Thanks Ryan, slindsey and others for your contributions.

@ruslanpa

This comment has been minimized.

Show comment Hide comment
@ruslanpa

ruslanpa Jul 24, 2014

Nice, very nice!

Nice, very nice!

@zzz6519003

This comment has been minimized.

Show comment Hide comment
@zzz6519003

zzz6519003 Aug 3, 2014

buy you a drink! 🐻

buy you a drink! 🐻

@danielricecodes

This comment has been minimized.

Show comment Hide comment
@danielricecodes

danielricecodes Aug 6, 2014

Thanks! I like to re-read these questions before an interview so that I can recall faster. Interviews can be stressful and it helps to go into them prepared. As a professional Rubyist I don't think about these questions all the time, so its helpful to review the basics so that I can give coherent, concise answers.

Thanks! I like to re-read these questions before an interview so that I can recall faster. Interviews can be stressful and it helps to go into them prepared. As a professional Rubyist I don't think about these questions all the time, so its helpful to review the basics so that I can give coherent, concise answers.

@Rameshwar007

This comment has been minimized.

Show comment Hide comment
@Rameshwar007

Rameshwar007 Aug 28, 2014

Excellent....!

Excellent....!

@t3en4

This comment has been minimized.

Show comment Hide comment
@t3en4

t3en4 Oct 1, 2014

Thank you!

t3en4 commented Oct 1, 2014

Thank you!

@kumardev

This comment has been minimized.

Show comment Hide comment
@kumardev

kumardev Oct 8, 2014

The gotApi link is dead , please do remove it .

kumardev commented Oct 8, 2014

The gotApi link is dead , please do remove it .

@xam7247

This comment has been minimized.

Show comment Hide comment
@xam7247

xam7247 Dec 5, 2014

Indeed a nice list, Ryan - Matches a lot with my own 😄 . To add, I also use the following to understand candidate's core concepts for senior roles:
super - use & purpose for classes
include & extend
mixins + variable scopes for mixed in modules
And a final touch up with meta-programming concepts.

Keep them coming.

xam7247 commented Dec 5, 2014

Indeed a nice list, Ryan - Matches a lot with my own 😄 . To add, I also use the following to understand candidate's core concepts for senior roles:
super - use & purpose for classes
include & extend
mixins + variable scopes for mixed in modules
And a final touch up with meta-programming concepts.

Keep them coming.

@jtorreggiani

This comment has been minimized.

Show comment Hide comment
@jtorreggiani

jtorreggiani Dec 15, 2014

Thank you! These questions were very helpful for preparing for job interviews. I have one suggestion. I coded along as I read over the gist and noticed that your unit test example was using test/unit instead of minitest. The interpreter responded by raising the warning "you should require 'minitest/autorun' instead." It might be helpful for others if you update the syntax for that example. Thanks again!

require 'minitest/autorun'

class Brokened
  def uh_oh
    "I needs fixing"
  end
end

class BrokenedTest < Minitest::Test
  def test_uh_of
    actual = Brokened.new
    assert_equal("I'm all better now", actual.uh_oh)
  end
end

#=> # Running:
#=> 
#=> F
#=> 
#=> Finished in 0.001451s, 689.1799 runs/s, 689.1799 assertions/s.
#=> 
#=> 1) Failure:
#=> BrokenedTest#test_uh_of [test.rb:13]:
#=> Expected: "I'm all better now"
#=> Actual: "I needs fixing"
#=> 
#=> 1 runs, 1 assertions, 1 failures, 0 errors, 0 skips

Thank you! These questions were very helpful for preparing for job interviews. I have one suggestion. I coded along as I read over the gist and noticed that your unit test example was using test/unit instead of minitest. The interpreter responded by raising the warning "you should require 'minitest/autorun' instead." It might be helpful for others if you update the syntax for that example. Thanks again!

require 'minitest/autorun'

class Brokened
  def uh_oh
    "I needs fixing"
  end
end

class BrokenedTest < Minitest::Test
  def test_uh_of
    actual = Brokened.new
    assert_equal("I'm all better now", actual.uh_oh)
  end
end

#=> # Running:
#=> 
#=> F
#=> 
#=> Finished in 0.001451s, 689.1799 runs/s, 689.1799 assertions/s.
#=> 
#=> 1) Failure:
#=> BrokenedTest#test_uh_of [test.rb:13]:
#=> Expected: "I'm all better now"
#=> Actual: "I needs fixing"
#=> 
#=> 1 runs, 1 assertions, 1 failures, 0 errors, 0 skips
@kumardev

This comment has been minimized.

Show comment Hide comment
@kumardev

kumardev Jan 19, 2015

gotAPI link isn't working , please replace with the working one .

gotAPI link isn't working , please replace with the working one .

@simkim

This comment has been minimized.

Show comment Hide comment
@simkim

simkim Jan 22, 2015

The ruby method accepts any Object and could return anything, but in this case will return a boolean if executed without exceptions (and not by a tortured mind).

class Foo 
    def ==(b)
        return 2
    end 
    def size()
        self
    end 
end

def empty?(s)
      return s.size == 0
end

puts empty?(Foo.new).class

print Fixnum

simkim commented Jan 22, 2015

The ruby method accepts any Object and could return anything, but in this case will return a boolean if executed without exceptions (and not by a tortured mind).

class Foo 
    def ==(b)
        return 2
    end 
    def size()
        self
    end 
end

def empty?(s)
      return s.size == 0
end

puts empty?(Foo.new).class

print Fixnum

@sangyongjung

This comment has been minimized.

Show comment Hide comment
@sangyongjung

sangyongjung Mar 12, 2015

Isn't

a ||= b

same as

a = a || b

instead of

a || a = b

??

Isn't

a ||= b

same as

a = a || b

instead of

a || a = b

??

@demonmind

This comment has been minimized.

Show comment Hide comment
@demonmind

demonmind Apr 15, 2015

if a is nil will be evaluated as false so
a || a = b
is the same as
a = a || b

if a is nil will be evaluated as false so
a || a = b
is the same as
a = a || b

@roshannangare

This comment has been minimized.

Show comment Hide comment
@roshannangare

roshannangare Jul 7, 2015

My company has created a Ruby test to test basic skills of Ruby developer. Please have a look and give your feedback.

My company has created a Ruby test to test basic skills of Ruby developer. Please have a look and give your feedback.

@sarbull

This comment has been minimized.

Show comment Hide comment
@sarbull

sarbull Jul 30, 2015

Very good question and example!

// Java
public boolean isEmpty(String s) {
  return s.length() == 0;
}
# ruby
def empty?(s)
  return s.size == 0
end

sarbull commented Jul 30, 2015

Very good question and example!

// Java
public boolean isEmpty(String s) {
  return s.length() == 0;
}
# ruby
def empty?(s)
  return s.size == 0
end
@RKushnir

This comment has been minimized.

Show comment Hide comment
@RKushnir

RKushnir Aug 10, 2015

Explain this ruby idiom: a ||= b
a || a = b

Wrong, you failed your own interview :)
When a is undefined, a ||= b succeeds, while a || a = b fails with undefined local variable.

Explain this ruby idiom: a ||= b
a || a = b

Wrong, you failed your own interview :)
When a is undefined, a ||= b succeeds, while a || a = b fails with undefined local variable.

@jyothu

This comment has been minimized.

Show comment Hide comment
@jyothu

jyothu Sep 9, 2015

@ RKushnir
No. If 'b' was defined, then It wont fail.

a || a = 10
=> 10

jyothu commented Sep 9, 2015

@ RKushnir
No. If 'b' was defined, then It wont fail.

a || a = 10
=> 10

This comment has been minimized.

Show comment Hide comment
@ghost

ghost Oct 27, 2015

@jyothu I tries above snippet and here is result @RKushnir is right.

2.2.1 :005 > a || a = 10
NameError: undefined local variable or method `a' for main:Object

ghost commented Oct 27, 2015

@jyothu I tries above snippet and here is result @RKushnir is right.

2.2.1 :005 > a || a = 10
NameError: undefined local variable or method `a' for main:Object
@AndreiMotinga

This comment has been minimized.

Show comment Hide comment
@AndreiMotinga

AndreiMotinga Nov 25, 2015

@zambrevishal,

2.2.0 :038 >   a = 10
10
2.2.0 :039 > a || a = 7
10
2.2.0 :040 >

@zambrevishal,

2.2.0 :038 >   a = 10
10
2.2.0 :039 > a || a = 7
10
2.2.0 :040 >
@2called-chaos

This comment has been minimized.

Show comment Hide comment
@2called-chaos

2called-chaos Jan 14, 2016

Also interesting in that matter. If you use a local variable which never got defined but has an assignment in the method itself even if it is impossible to reach you will get nil after that "assignment".

def foo
  # using a here would raise an exception
  if false
    a = 1 # after that point it returns nil aside the fact that it never actually got defined
  end
  a
end
foo => nil

Also interesting in that matter. If you use a local variable which never got defined but has an assignment in the method itself even if it is impossible to reach you will get nil after that "assignment".

def foo
  # using a here would raise an exception
  if false
    a = 1 # after that point it returns nil aside the fact that it never actually got defined
  end
  a
end
foo => nil
@jenmitchell024

This comment has been minimized.

Show comment Hide comment
@jenmitchell024

jenmitchell024 Jan 29, 2016

Hello,
I am an IT Recruiter and we are having a hard time finding solid Ruby Developers. The challenges my client has indicated is that they cannot find Ruby Developers with full-stack experience, specifically. Does anyone have any thoughts on this you might be able to share? Also, I have seen a lot of Automation Testers using Ruby to build automation testing frameworks....would this experience translate into a Ruby Developer role or how extensive would be the learning curve? What about if someone has experience with Ruby scripting on the server side (ie used for automating builds/deployment)? Thank you for your help!!

Hello,
I am an IT Recruiter and we are having a hard time finding solid Ruby Developers. The challenges my client has indicated is that they cannot find Ruby Developers with full-stack experience, specifically. Does anyone have any thoughts on this you might be able to share? Also, I have seen a lot of Automation Testers using Ruby to build automation testing frameworks....would this experience translate into a Ruby Developer role or how extensive would be the learning curve? What about if someone has experience with Ruby scripting on the server side (ie used for automating builds/deployment)? Thank you for your help!!

@Drenmi

This comment has been minimized.

Show comment Hide comment
@Drenmi

Drenmi Feb 22, 2016

You missed one of the most interesting subtleties about the question: "What is a class?", namely that in Ruby, a class is also an object (of type Class.)

Drenmi commented Feb 22, 2016

You missed one of the most interesting subtleties about the question: "What is a class?", namely that in Ruby, a class is also an object (of type Class.)

@aparna951992

This comment has been minimized.

Show comment Hide comment
@aparna951992

aparna951992 Apr 8, 2016

Thanks bro... this helps... Hope you would come out with more such posts.

Thanks bro... this helps... Hope you would come out with more such posts.

@agnel

This comment has been minimized.

Show comment Hide comment
@agnel

agnel Jul 1, 2016

Its very helpful. Thanks.
But I think there are more topics which can be covered.
Can you please revise this to cover a broad range ?

agnel commented Jul 1, 2016

Its very helpful. Thanks.
But I think there are more topics which can be covered.
Can you please revise this to cover a broad range ?

@sajjadmurtaza

This comment has been minimized.

Show comment Hide comment
@sajjadmurtaza

sajjadmurtaza Aug 2, 2016

Can you give me examples and explanations for
Abstraction.
Encapsulation.
Inheritance.
Polymorphism.
in ruby?

Can you give me examples and explanations for
Abstraction.
Encapsulation.
Inheritance.
Polymorphism.
in ruby?

@lankireddy

This comment has been minimized.

Show comment Hide comment
@lankireddy

lankireddy Apr 24, 2017

i want ror depth tutorials plz reference links send to me
id withsrinu@yahoo.com
withsrinu@gmail.com

i want ror depth tutorials plz reference links send to me
id withsrinu@yahoo.com
withsrinu@gmail.com

@lankireddy

This comment has been minimized.

Show comment Hide comment
@lankireddy

lankireddy Apr 24, 2017

inheritence is accirie the properties of another obl\ject
parent->child process

inheritence is accirie the properties of another obl\ject
parent->child process

@david-dcp

This comment has been minimized.

Show comment Hide comment
@david-dcp

david-dcp Jul 17, 2017

Really great page - thanks for sharing.

The gotAPI domain seems to now be a Japanese soft porn site. :/ Might want to update this.

screenshot 2017-07-17 13 04 57

Really great page - thanks for sharing.

The gotAPI domain seems to now be a Japanese soft porn site. :/ Might want to update this.

screenshot 2017-07-17 13 04 57

@tomlobato

This comment has been minimized.

Show comment Hide comment
@tomlobato

tomlobato Aug 6, 2017

An important aspect of Proc`s that could be mentioned here is its associated binding.

An important aspect of Proc`s that could be mentioned here is its associated binding.

@marcelorxaviers

This comment has been minimized.

Show comment Hide comment
@marcelorxaviers

marcelorxaviers Aug 7, 2017

You can invoke the method as 4th way tho:

class Some
  def blah
    puts "blah"
  end  
end

[2] pry(main)> Some.new.blah
blah
=> nil
[3] pry(main)> Some.new::blah
blah
=> nil  

marcelorxaviers commented Aug 7, 2017

You can invoke the method as 4th way tho:

class Some
  def blah
    puts "blah"
  end  
end

[2] pry(main)> Some.new.blah
blah
=> nil
[3] pry(main)> Some.new::blah
blah
=> nil  
@piyushawasthi

This comment has been minimized.

Show comment Hide comment
@piyushawasthi

piyushawasthi Aug 29, 2017

@zambrevishal : because a is not define in || operator that why it's throwing Name error. NameError: undefined local variable or methoda' for main:Object`

2.4.0 :002 > a
=> nil
2.4.0 :003 > a || a = 10
=> 10

@zambrevishal : because a is not define in || operator that why it's throwing Name error. NameError: undefined local variable or methoda' for main:Object`

2.4.0 :002 > a
=> nil
2.4.0 :003 > a || a = 10
=> 10

@z0ny

This comment has been minimized.

Show comment Hide comment
@z0ny

z0ny Sep 10, 2017

// Java
public boolean isEmpty(String s) {
  return s.length() == 0;
}
# ruby
def empty?(s)
  return s.size == 0
end

Nice example but you cant be sure if returned value in ruby is boolean even in your case because we dont know the input.

class X
 def ==(_)
  'not a boolean'
 end

 def size
  self
 end
end

def empty?(a)
 a.size == 0
end

empty?(X.new)
=> "not a boolean"

EDIT: Ah now i see that simkim wrote it already

z0ny commented Sep 10, 2017

// Java
public boolean isEmpty(String s) {
  return s.length() == 0;
}
# ruby
def empty?(s)
  return s.size == 0
end

Nice example but you cant be sure if returned value in ruby is boolean even in your case because we dont know the input.

class X
 def ==(_)
  'not a boolean'
 end

 def size
  self
 end
end

def empty?(a)
 a.size == 0
end

empty?(X.new)
=> "not a boolean"

EDIT: Ah now i see that simkim wrote it already

@devquora

This comment has been minimized.

Show comment Hide comment
@adityavarma1234

This comment has been minimized.

Show comment Hide comment
@adityavarma1234

adityavarma1234 Mar 20, 2018

Glad to know that I am not the only one who was going through this feeling.

Glad to know that I am not the only one who was going through this feeling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment