Creating a function in ruby

I have some problem creating a function for my Rails application.

I want it to work as follows:

str = "string here"
if str.within_range?(3..30)
puts "It withing the range!"
end

To do this, I added this to my application helper:

def within_range?(range)
  if self.is_a?(String)
    (range).include?(self.size)
  elsif self.is_a?(Integer)
    (range).include?(self)
  end
end

I get this error:

undefined method `within_range?' for "":String

Does anyone know what the problem is?

+3
source share
5 answers

Hello, I have a problem creating a function for my Rails application.

Uh, right there we come across our first misunderstanding: Ruby is not a functional programming language. It is an object oriented programming language. There is no function like function in Ruby. Ruby only uses methods that live on classes.

str = 'string here'
puts "It within the range!" if str.within_range?(3..30)

, , , . , String ( ).

, :

def within_range?(range)
  if is_a?(String)
    range.include?(size)
  elsif is_a?(Integer)
    range.include?(self) 
  end
end

within_range? , String. "" Object, () . , within_range? , , .

, :

:

undefined method `within_range?' for "":String

: -, , Ruby , "string here":String, "":String. , within_range? , 'string here', . , - , , , within_range? .

, . Ruby , Ruby within_range? , .. within_range? String , Object. ! ( ) Object. , , :

NoMethodError: private method `within_range?' called for "string here":String

, , , . Ruby on Rails, , .

. , String. , , , String Integer, , Object:

class Object
  def within_range?(range)
    if    is_a?(String)  then range.include?(size)
    elsif is_a?(Integer) then range.include?(self) end
  end
end

[ , self .]

. -: is_a? Ruby - . , - , , , - "-Rubyish", , . ( : , , , Ruby.)

, is_a? kind_of? Module#===, .

, , self. , case if:

class Object
  def within_range?(range)
    case self
    when String  then range.include?(size)
    when Integer then range.include?(self) end
  end
end

, . ! , . , , : , ! ( , , , , ...)

, , . : foo.bar, Ruby bar, foo. , .

, : ? , , , , . , Ruby , :

class String
  def within_range?(range)
    range.include?(size)
  end
end

class Integer
  def within_range?(range)
    range.include?(self)
  end
end

. : , within_range? , , , , , NoMethodError:

[].within_range?(3..30)
# => NoMethodError: undefined method `within_range?' for []:Array

else, nil, false , , , , .

: , , , . , Integer Numeric.

: , , , , Object.

, , : , String , , , .

( , Object), , ( ) . , , , . :

  2.within_range?(1..3)     # => true
'B'.within_range?('A'..'C') # => false

, , , , true. , B A C, .

: , . , , , , , , , .

length_within_range?, :

'B'.length_within_range?(1..2) # => true
'B'.within_range?('A'..'C')    # => true

[, within_range? Object.]

, , , .

str.length_within_range?(3..30)
str.length.within_range?(3..30)

, , Object.

Object, , , ,

(3..30).include?(str.length)

.

+4

, , - . String.

within_range? , .

include, String.include String Integer, send.

String.send(:include, YourModule)
Integer.send(:include, YourModule)

, String Integer , YourModule.

: Rubys Mixins Rails Java Framework

+5

- . , Object, is_a. :

class Object
  def within_range?(range)
    if is_a?(String)
      (range).include?(size)
    elsif is_a?(Integer)
      (range).include?(self)
    end
  end
end
+4

, - . , OO, :

def within_range?(obj, range)
  case obj
  when String  then range.include?(obj.size)
  when Integer then range.include?(obj)
  end
end

:

str = "string here"
puts "'#{str}' is within the range!" if within_range?(str, 3..30)
0

, , 'inside_range' String. String.

class String
 def within_range?(range)
  if self.is_a?(String)
    (range).include?(self.size)
  elsif self.is_a?(Integer)
    (range).include?(self)
  end
 end
end
-1

Source: https://habr.com/ru/post/1741944/


All Articles