A cheat sheet for Java experienced people to learn Ruby (rails)

* For updating from time to time </ font>

Target audience

・ I ・ Experienced in Java and C #

Background ~ Rails tutorial The amount of information is too huge Problem ~

・ The amount of information is tremendous. If you think that the amount of information is too large and you are doing it in order from the top (while doing business), it will not end. ・ Although there is already a similar article, I decided to make a dictionary-like thing in my own words, "This is Java."

Below is the dictionary

・ * Strictly speaking, it may not be equal, but I feel that it is posted with the feeling that it is this if you force it. ・ ※ I would like you to point out if there is something that "the understanding is wrong" because you are learning. -Since the volume is small at this stage, the concept unique to ruby is also included.

  • initialize -Initialize method (the first method to be called when creating an instance) ・ ** Constructor in java ** ・ See ** test.rb ** </ font>
  • attr_accessor ・ Accessor method -** lombok in java (Spring) (the one included in the library for defining getters / setters in the class) ** -** For defining instance variables ** ・ See ** test.rb ** </ font>
  • self ・ ~~ T Used in many ways ~~ ・ What becomes self depends on where self is used -Used when specifying variables and methods inside the class (** Class variables use @@ described later **) -Specify the field specified by attr_accessor -It is also possible to show the caller of the method (description method in ** test.rb **" ・ ** This ** in java ・ See ** test.rb ** </ font>

test.rb



class Car
  #Object variable (value)
  # type(Car type),weight(weight),user(User)
  attr_accessor :type,:weight,:user

  #Special method for initialization
  def initialize
    self.type = 'SUV'
    self.weight = 2500
  end

  #Object method (processing)
  def description(user)
    puts "Hello#{user},This car type is#{type},Weight is#{power}is."
  end
end

#Create an instance and assign it to a variable
Car_A = Car.new

Car_A.description('I')

p Car_A

-@ (At sign)

・ The one attached to the instance variable ・ Agree with java instance variable * 1)

    1. ... A field variable associated with an instance, which is a variable of each instance created by "new". ・ ** See test2.rb ** </ font> * You can write like this

test2.rb



class Car
  #Object variable (value)
  # type(Car type),weight(weight),user(User)
  attr_accessor :type,:weight,:user

  #Special method for initialization
  def initialize
    @type = 'SUV'
    @weight = 2500
  end

  #Object method (processing)
  def description(user)
    puts "Hello#{user},This car type is#{@type},Weight is#{@power}is."
  end
end

#Create an instance and assign it to a variable
Car_A = Car.new

Car_A.description('I')

p Car_A

-@@ (two at signs)

・ The one attached to the class variable -Can be used in classes and instances ・ Values are shared ・ See ** test3.rb ** </ font>

test3.rb



class Test
  @@class_var = 0

  def foo
    @@class_var += 1
    p @@class_var
  end

  def self.hoge
    @@class_var += 1
    p @@class_var
  end
end

test = Test.new

#Values are shared within the same instance
test.foo # => 1
test.foo # => 2

#Values are shared even if the instances are different
test2 = Foo.new
test2.foo # => 3

#It can be accessed from class methods and the values are shared.
Test.hoge # => 4

  • require

・ I wanted to write import ... in java, but in reality, require seems to have a higher degree of freedom (see the link below). ・ [Ruby] Ruby require is different from Java

- module

・ I couldn't find this in Java. .. ..

-** Difference from class ** --Unable to instantiate. --Cannot be inherited.

-** Usage ** --Namespace (Ensuring method uniqueness ... To prevent duplication of method names when importing libraries, etc.) --Mix-in (Use by including all methods in a module)

Recommended Posts